-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: 세션 만료 및 로그아웃 처리를 AuthState 기반으로 재구현 #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.smtm.pickle.domain.model.auth | ||
|
|
||
| sealed interface AuthState { | ||
| data object Authenticated : AuthState | ||
| data object Unauthenticated : AuthState | ||
| data object SessionExpired : AuthState | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.smtm.pickle.presentation | ||
|
|
||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.smtm.pickle.domain.model.auth.AuthState | ||
| import com.smtm.pickle.domain.provider.TokenProvider | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import kotlinx.coroutines.channels.Channel | ||
| import kotlinx.coroutines.flow.receiveAsFlow | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
| class AppViewModel @Inject constructor( | ||
| private val tokenProvider: TokenProvider, | ||
| ) : ViewModel() { | ||
|
|
||
| private val _authNavEvent = Channel<AuthNavEvent>(Channel.CONFLATED) | ||
| val authNavEvent = _authNavEvent.receiveAsFlow() | ||
|
|
||
| init { | ||
| viewModelScope.launch { | ||
| tokenProvider.getAuthStateFlow().collect { state -> | ||
| when (state) { | ||
| AuthState.SessionExpired -> _authNavEvent.trySend(AuthNavEvent.ToLoginWithMessage) | ||
| AuthState.Unauthenticated -> _authNavEvent.trySend(AuthNavEvent.ToLogin) | ||
| else -> {} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sealed interface AuthNavEvent { | ||
| data object ToLogin : AuthNavEvent | ||
| data object ToLoginWithMessage : AuthNavEvent | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.smtm.pickle.presentation | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel | ||
| import androidx.navigation.compose.rememberNavController | ||
| import com.smtm.pickle.presentation.designsystem.components.snackbar.PickleSnackbar | ||
| import com.smtm.pickle.presentation.designsystem.components.snackbar.SnackbarHost | ||
| import com.smtm.pickle.presentation.designsystem.components.snackbar.model.SnackbarState | ||
| import com.smtm.pickle.presentation.navigation.PickleNavHost | ||
| import com.smtm.pickle.presentation.navigation.route.LoginRoute | ||
|
|
||
| @Composable | ||
| fun PickleApp( | ||
| appViewModel: AppViewModel = hiltViewModel(), | ||
| ) { | ||
| val navController = rememberNavController() | ||
| val snackbarState = remember { SnackbarState() } | ||
| val sessionExpiredMessage = stringResource(R.string.global_session_expired) | ||
|
|
||
| LaunchedEffect(Unit) { | ||
| appViewModel.authNavEvent.collect { event -> | ||
| navController.navigate(LoginRoute) { | ||
| popUpTo(navController.graph.id) { inclusive = true } | ||
| } | ||
| if (event is AuthNavEvent.ToLoginWithMessage) { | ||
| snackbarState.show( | ||
| PickleSnackbar.snackbarShort(message = sessionExpiredMessage) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| PickleNavHost(navController = navController) | ||
| SnackbarHost(snackbarState) | ||
|
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
참고: Box {
PickleNavHost(navController = navController)
SnackbarHost(snackbarState)
} |
||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.