Профиль, редактирование без аватара
This commit is contained in:
@@ -6,6 +6,9 @@ import auth.domain.model.AuthResult
|
||||
import auth.domain.repository.AuthRepository
|
||||
import core.network.ServerConfig
|
||||
import core.security.TokenManager
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import javax.inject.Inject
|
||||
|
||||
class AuthRepositoryImpl @Inject constructor(
|
||||
@@ -14,6 +17,8 @@ class AuthRepositoryImpl @Inject constructor(
|
||||
private val serverConfig: ServerConfig
|
||||
) : AuthRepository {
|
||||
|
||||
private val _isAuthenticated = kotlinx.coroutines.flow.MutableStateFlow(tokenManager.getToken() != null)
|
||||
|
||||
override suspend fun login(userName: String, password: String): Result<AuthResult> {
|
||||
return try {
|
||||
val response = api.login(AuthRequest(userName, password))
|
||||
@@ -21,6 +26,7 @@ class AuthRepositoryImpl @Inject constructor(
|
||||
val userId = response.userId ?: ""
|
||||
|
||||
tokenManager.saveToken(token, userId)
|
||||
_isAuthenticated.value = true
|
||||
fetchConfig()
|
||||
Result.success(
|
||||
AuthResult(
|
||||
@@ -43,6 +49,7 @@ class AuthRepositoryImpl @Inject constructor(
|
||||
val userId = response.userId ?: ""
|
||||
|
||||
tokenManager.saveToken(token, userId)
|
||||
_isAuthenticated.value = true
|
||||
fetchConfig()
|
||||
Result.success(
|
||||
AuthResult(
|
||||
@@ -60,10 +67,15 @@ class AuthRepositoryImpl @Inject constructor(
|
||||
|
||||
override suspend fun logout() {
|
||||
tokenManager.deleteToken()
|
||||
_isAuthenticated.value = false
|
||||
}
|
||||
|
||||
override fun isAuthenticated(): Boolean {
|
||||
return tokenManager.getToken() != null
|
||||
return _isAuthenticated.value
|
||||
}
|
||||
|
||||
override fun isAuthenticatedFlow(): kotlinx.coroutines.flow.StateFlow<Boolean> {
|
||||
return _isAuthenticated.asStateFlow()
|
||||
}
|
||||
|
||||
override suspend fun fetchConfig(): Result<Unit> {
|
||||
|
||||
@@ -8,5 +8,6 @@ interface AuthRepository {
|
||||
suspend fun logout()
|
||||
suspend fun fetchConfig(): Result<Unit>
|
||||
fun isAuthenticated(): Boolean
|
||||
fun isAuthenticatedFlow(): kotlinx.coroutines.flow.StateFlow<Boolean>
|
||||
suspend fun updatePushToken(token: String)
|
||||
}
|
||||
|
||||
@@ -23,15 +23,20 @@ class AuthViewModel @Inject constructor(
|
||||
private val repository: AuthRepository
|
||||
) : ViewModel() {
|
||||
|
||||
init {
|
||||
if (repository.isAuthenticated()) {
|
||||
updatePushToken()
|
||||
}
|
||||
}
|
||||
|
||||
private val _state = MutableStateFlow(AuthState(isAuthenticated = repository.isAuthenticated()))
|
||||
val state: StateFlow<AuthState> = _state.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
repository.isAuthenticatedFlow().collect { authenticated ->
|
||||
_state.update { it.copy(isAuthenticated = authenticated) }
|
||||
if (authenticated) {
|
||||
updatePushToken()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun checkAuth() {
|
||||
_state.update { it.copy(isAuthenticated = repository.isAuthenticated()) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user