Skip to content

Commit

Permalink
Implement authorization check in activity onResume()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandr7035 committed Feb 9, 2022
1 parent f6318e5 commit 4e686fe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,52 +61,49 @@ class MainActivity : AppCompatActivity() {
binding.bottomNavigationView.isGone = !bottomNavigationDestinations.contains(destination.id)
}

// navigateSafe method allow us not think about current
// It will be changed only if we at login fragment (on app started)
// navigateSafe method allow us not check current destination
viewModel.getAuthCheckLiveData().observe(this, { authCheckResult ->
binding.progressView.root.isVisible = false

when (authCheckResult) {
is AuthCheckResModel.Success -> {
Timber.debug("AUTH_CHECK success")
// Means token did not expired. Allow to go in the profile
// Means token did not expired
navController.navigateSafe(LoginFragmentDirections.actionLoginFragmentToProfileFragment())
}

// When fail, it depends.
is AuthCheckResModel.Fail -> {
when (authCheckResult.errorType) {
// That means token has expired
// Stay on login fragment and show message
// Stay on login fragment and show error message
ErrorType.AUTHORIZATION_ERROR -> {

if (navController.currentDestination?.id != R.id.loginFragment) {

Timber.debug("AUTH_CHECK expired")
binding.root.showSnackBar(
getString(R.string.eror_auth),
SnackBarMode.Negative,
Snackbar.LENGTH_LONG
)

// Go to login fragment (if not here)
navController.navigateSafe(MainActivityDirections.actionGlobalLoginFragment())
}
Timber.debug("AUTH_CHECK expired")
binding.root.showSnackBar(
getString(R.string.eror_auth),
SnackBarMode.Negative,
Snackbar.LENGTH_LONG
)

// Go to login fragment (if not here)
navController.navigateSafe(MainActivityDirections.actionGlobalLoginFragment())
}

// No connection with the internet
// But we can let the user inside as some of his data is cached
ErrorType.FAILED_CONNECTION -> {
Timber.debug("AUTH_CHECK connection")
Timber.debug("AUTH_CHECK connection error")
navController.navigateSafe(LoginFragmentDirections.actionLoginFragmentToProfileFragment())
}

// Unknown error. Show corresponding fragment
else -> {
navController.navigateSafe(LoginFragmentDirections.actionGlobalErrorFragment(
getString(R.string.error_unknown_title),
getString(R.string.error_unknown)
))
navController.navigateSafe(
LoginFragmentDirections.actionGlobalErrorFragment(
getString(R.string.error_unknown_title),
getString(R.string.error_unknown)
)
)
}
}
}
Expand All @@ -117,21 +114,15 @@ class MainActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()

Timber.debug("AUTH_CHECK activity onResume")

// if (navController.currentDestination?.id != R.id.loginFragment) {

// Affinidi token expires in 1 hour
// So we should check for auth on start
// and if token expired show login fragment
//
// If no token just show login fragment
if (viewModel.checkIfPreviouslyAuthorized()) {
Timber.debug("AUTH_CHECK start")
binding.progressView.root.isVisible = true
viewModel.startAuthCheck()
}
// Affinidi token expires in 1 hour
// So we should check for auth on start
// and if token expired show login fragment (or stay if already there)
if (viewModel.checkIfPreviouslyAuthorized()) {
Timber.debug("AUTH_CHECK start")
binding.progressView.root.isVisible = true
viewModel.startAuthCheck()
}
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import by.alexandr7035.affinidi_id.core.livedata.SingleLiveEvent
import by.alexandr7035.affinidi_id.domain.core.ErrorType
import by.alexandr7035.affinidi_id.domain.model.auth_check.AuthCheckResModel
import by.alexandr7035.affinidi_id.domain.usecase.user.AuthCheckUseCase
import by.alexandr7035.affinidi_id.domain.usecase.user.GetAuthStateUseCase
Expand All @@ -31,9 +30,7 @@ class MainViewModel @Inject constructor(
val res = authCheckUseCase.execute()

withContext(Dispatchers.Main) {
// authCheckLiveData.value = res
// FIXME debug
authCheckLiveData.value = AuthCheckResModel.Fail(ErrorType.AUTHORIZATION_ERROR)
authCheckLiveData.value = res
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class LoginRepositoryImpl @Inject constructor(private val userApiService: UserAp
}

override suspend fun logOut(accessToken: String): LogOutModel {
Timber.debug("AUTH_CHECK logout called")

try {
val res = userApiService.logOut(accessToken)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package by.alexandr7035.affinidi_id.domain.usecase.user
import by.alexandr7035.affinidi_id.domain.core.ErrorType
import by.alexandr7035.affinidi_id.domain.model.auth_check.AuthCheckResModel
import by.alexandr7035.affinidi_id.domain.repository.AuthCheckRepository
import by.alexandr7035.affinidi_id.domain.repository.LoginRepository
import javax.inject.Inject

// Ass accessToken expires fastly we need to check for auth on app started
Expand All @@ -17,6 +16,7 @@ class AuthCheckUseCase @Inject constructor(
val authState = getAuthStateUseCase.execute()

val authCheckResult = authCheckRepository.makeCheckRequest(authState)
// val authCheckResult= AuthCheckResModel.Fail(ErrorType.AUTHORIZATION_ERROR)

if (authCheckResult is AuthCheckResModel.Fail) {
if (authCheckResult.errorType == ErrorType.AUTHORIZATION_ERROR) {
Expand Down

0 comments on commit 4e686fe

Please sign in to comment.