Skip to content

Commit

Permalink
Merge pull request #45 from SSU-Plector/feat/#43-auto-login
Browse files Browse the repository at this point in the history
[Feat/#43 auto login] 자동 로그인 구현 및 리프레쉬 토큰 만료 수정
  • Loading branch information
kangyuri1114 authored Jul 7, 2024
2 parents 6f83f3a + 4543904 commit 36c5e01
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
91 changes: 58 additions & 33 deletions core/network/src/main/java/com/sample/network/SPAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,72 @@ import okhttp3.Authenticator
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SPAuthenticator @Inject constructor(
private val dataStore: NetworkPreference,
private val api: AuthService,
@ApplicationContext private val context: Context,
private val navigationProvider: NavigationProvider,
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
// if (response.request.header("Authorization") == null) {
// return null
// }
class SPAuthenticator
@Inject
constructor(
private val dataStore: NetworkPreference,
private val api: AuthService,
@ApplicationContext private val context: Context,
private val navigationProvider: NavigationProvider,
) : Authenticator {
override fun authenticate(
route: Route?,
response: Response,
): Request? {
if (response.request.header("Authorization") == null) {
return null
}

if (response.code == 401) {
val refreshToken = dataStore.refreshToken
val newTokens = runCatching {
runBlocking {
api.refreshToken(refreshToken)
val responseBody = response.peekBody(Long.MAX_VALUE).string()
val isAuthError =
try {
val jsonObject = JSONObject(responseBody)
jsonObject.getString("code") == "AUTH4001"
} catch (e: JSONException) {
false
}
}.onSuccess {
val data = it.data
dataStore.refreshToken = data.refreshToken.orEmpty()
dataStore.accessToken = data.accessToken.orEmpty()
}.onFailure {
Timber.e("Authenticator", it.toString())
runBlocking {
dataStore.clear()
UserApiClient.instance.logout { error ->
Timber.e("Authenticator", error.toString())
ProcessPhoenix.triggerRebirth(context, navigationProvider.toLogin())

if (isAuthError) {
return handleTokenRefresh(response)
}

return null
}

private fun handleTokenRefresh(response: Response): Request? {
val refreshToken = dataStore.refreshToken
val newTokens =
runCatching {
runBlocking {
api.refreshToken(refreshToken)
}
}
}.getOrThrow()
}.onSuccess {
val data = it.data
dataStore.refreshToken = data.refreshToken.orEmpty()
dataStore.accessToken = data.accessToken.orEmpty()
}.onFailure {
Timber.e("Authenticator", it.toString())
runBlocking {
dataStore.clear()
UserApiClient.instance.logout { error ->
Timber.e("Authenticator", error.toString())
ProcessPhoenix.triggerRebirth(context, navigationProvider.toLogin())
}
}
}.getOrThrow()

return response.request.newBuilder()
// .header("Authorization", newTokens.data.accessToken)
.build()
return newTokens.data.accessToken?.let {
response.request
.newBuilder()
.header("Authorization", "Bearer $it")
.build()
}
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class LoginActivity @Inject constructor(
super.onCreate(savedInstanceState)
setContentView(binding.root)

autoLogin()
kakaoLogin()
collectKakaoLogin()
setLoginViewPager()
Expand All @@ -61,6 +62,14 @@ class LoginActivity @Inject constructor(
}.launchIn(lifecycleScope)
}

private fun autoLogin() {
if (viewModel.autoLoginConfigured.value) {
navigateToMain()
} else {
Timber.d("Auto Login Configured False")
}
}

private fun navigateToMain() {
intent = Intent(this, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class LoginViewModel @Inject constructor(
private val _isLogin = MutableStateFlow(false)
val isLogin = _isLogin.asStateFlow()

private val _autoLoginConfigured = MutableStateFlow(networkPreference.autoLoginConfigured)
val autoLoginConfigured = _autoLoginConfigured.asStateFlow()

init {
//getKakaoUserInfo()
_autoLoginConfigured.value = networkPreference.autoLoginConfigured
}

fun loginWithKakaoApp(context: Context) {
Expand Down

0 comments on commit 36c5e01

Please sign in to comment.