Skip to content
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

fix: 토큰 401 에러 해결 #562

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.into.websoso.data.authenticator

import android.content.Context
import com.kakao.sdk.user.UserApiClient
import com.into.websoso.data.repository.AuthRepository
import com.into.websoso.ui.login.LoginActivity
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
import okhttp3.Authenticator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ class AuthRepository @Inject constructor(
refreshToken = response.refreshToken
response.authorization
}.getOrElse {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [ktlint] standard:no-empty-first-line-in-method-block reported by reviewdog 🐶
First line in a method block should not be empty

it.printStackTrace()
null
}
}


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [ktlint] standard:no-consecutive-blank-lines reported by reviewdog 🐶
Needless blank line(s)

fun updateAccessToken(accessToken: String) {
this.accessToken = accessToken
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ class VersionRepository @Inject constructor(
companion object {
private const val OS = "android"
}
}
}
42 changes: 26 additions & 16 deletions app/src/main/java/com/into/websoso/ui/splash/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.into.websoso.databinding.ActivityLoginBinding
import com.into.websoso.ui.login.LoginActivity
import com.into.websoso.ui.main.MainActivity
import com.into.websoso.ui.splash.dialog.MinimumVersionDialogFragment
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -21,27 +22,42 @@ class SplashActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_spla
super.onCreate(savedInstanceState)

setupObserver()
splashViewModel.autoLogin()
}

private fun setupObserver() {
splashViewModel.isUpdateRequired.observe(this) { isUpdateRequired ->
if (isUpdateRequired) {
showMinimumVersionDialog()
return@observe
}
splashViewModel.updateMyProfile()
}

splashViewModel.error.observe(this) { isError ->
if (isError) {
UserApiClient.instance.logout {
startActivity(LoginActivity.getIntent(this))
}
}
}

splashViewModel.isAutoLogin.observe(this) { isAutoLogin ->
lifecycleScope.launch {
delay(1000L)
splashViewModel.updateMinimumVersion { isUpdateRequired ->
if (isUpdateRequired) {
showMinimumVersionDialog()
} else {
when (isAutoLogin) {
true -> navigateToMainActivity()
false -> navigateToLoginActivity()
}
}
when (isAutoLogin) {
true -> navigateToMainActivity()
false -> navigateToLoginActivity()
}
}
}
}

private fun showMinimumVersionDialog() {
val dialog = MinimumVersionDialogFragment.newInstance()
dialog.isCancelable = false
dialog.show(supportFragmentManager, MINIMUM_VERSION_TAG)
}

private fun navigateToMainActivity() {
startActivity(MainActivity.getIntent(this, true))
finish()
Expand All @@ -52,12 +68,6 @@ class SplashActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_spla
finish()
}

private fun showMinimumVersionDialog() {
val dialog = MinimumVersionDialogFragment.newInstance()
dialog.isCancelable = false
dialog.show(supportFragmentManager, MINIMUM_VERSION_TAG)
}

companion object {
private const val MINIMUM_VERSION_TAG = "MinimumVersionDialog"
}
Expand Down
53 changes: 38 additions & 15 deletions app/src/main/java/com/into/websoso/ui/splash/SplashViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.into.websoso.data.repository.AuthRepository
import com.into.websoso.data.repository.UserRepository
import com.into.websoso.data.repository.VersionRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
Expand All @@ -14,12 +15,46 @@ import javax.inject.Inject
class SplashViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val versionRepository: VersionRepository,
private val userRepository: UserRepository,
) : ViewModel() {
private val _isUpdateRequired: MutableLiveData<Boolean> = MutableLiveData()
val isUpdateRequired: LiveData<Boolean> get() = _isUpdateRequired

private var _isAutoLogin = MutableLiveData(false)
private var _isAutoLogin: MutableLiveData<Boolean> = MutableLiveData()
val isAutoLogin: LiveData<Boolean> get() = _isAutoLogin

fun autoLogin() {
private var _error: MutableLiveData<Boolean> = MutableLiveData(false)
val error: LiveData<Boolean> get() = _error

init {
checkAndUpdateVersion()
}

private fun checkAndUpdateVersion() {
viewModelScope.launch {
runCatching {
versionRepository.isUpdateRequired()
}.onSuccess { isRequired ->
_isUpdateRequired.value = isRequired
}
}
}

/* 토큰 만료 확인용 - 추후 로직 수정 필요 */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [ktlint] standard:no-single-line-block-comment reported by reviewdog 🐶
Replace the block comment with an EOL comment

fun updateMyProfile() {
viewModelScope.launch {
runCatching {
userRepository.fetchMyProfile()
}.onSuccess {
autoLogin()
}.onFailure {
authRepository.clearTokens()
_error.value = true
}
}
}

private fun autoLogin() {
viewModelScope.launch {
if (authRepository.isAutoLogin) {
runCatching {
Expand All @@ -34,16 +69,4 @@ class SplashViewModel @Inject constructor(
}
}
}

fun updateMinimumVersion(onUpdateRequired: (Boolean) -> Unit) {
viewModelScope.launch {
runCatching {
versionRepository.isUpdateRequired()
}.onSuccess { isUpdateRequired ->
onUpdateRequired(isUpdateRequired)
}.onFailure {
onUpdateRequired(false)
}
}
}
}
}
Loading