Skip to content

Commit

Permalink
#86 feat : applicationContext 로 accessToken 재발급 오류 시 IntroActivity 로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
yy0ung committed Nov 25, 2023
1 parent b01a522 commit 2a11fbc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Aos/app/src/main/java/com/avengers/nibobnebob/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import dagger.hilt.android.HiltAndroidApp
@HiltAndroidApp
class App : Application(){

init {
instance = this
}

override fun onCreate() {
super.onCreate()
initializeNaverSDK()
}

companion object{
lateinit var instance : App
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = APP_NAME)
fun getContext(): Context = instance.applicationContext
}

private fun initializeNaverSDK(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.avengers.nibobnebob.config

import android.util.Log
import com.avengers.nibobnebob.app.DataStoreManager
import com.avengers.nibobnebob.presentation.util.Constants.ACCESS
import com.avengers.nibobnebob.presentation.util.Constants.AUTHORIZATION
import com.avengers.nibobnebob.presentation.util.Constants.BEARER
import kotlinx.coroutines.flow.first
Expand All @@ -18,11 +16,10 @@ class AccessTokenInterceptor @Inject constructor(private val dataStoreManager: D
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {

// 동기가 아닌 비동기로 불러와야한다.. runBlocking말고 다른 방안에 대해 고민
val accessToken = runBlocking {
dataStoreManager.getAccessToken().first()
}
Log.d("토큰 테스트",accessToken.toString())

val builder: Request.Builder = chain.request().newBuilder()
accessToken?.takeIf { it.isNotEmpty() }?.let {
builder.addHeader(AUTHORIZATION, "$BEARER $it")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.avengers.nibobnebob.config


import android.content.Intent
import com.avengers.nibobnebob.app.App
import com.avengers.nibobnebob.app.DataStoreManager
import com.avengers.nibobnebob.data.model.BaseState
import com.avengers.nibobnebob.data.model.request.RefreshTokenRequest
import com.avengers.nibobnebob.data.model.response.BaseResponse
import com.avengers.nibobnebob.data.model.response.NaverLoginResponse
import com.avengers.nibobnebob.data.model.runRemote
import com.avengers.nibobnebob.data.remote.RefreshApi
import com.avengers.nibobnebob.presentation.ui.intro.IntroActivity
import com.avengers.nibobnebob.presentation.util.Constants
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -48,11 +51,15 @@ class BearerInterceptor @Inject constructor(
else -> {
dataStoreManager.deleteAccessToken()
dataStoreManager.deleteRefreshToken()

val intent = Intent(App.getContext(), IntroActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
App.getContext().startActivity(intent)
}
}
}
}
(newAccessToken)?.let {
newAccessToken?.let {
val newRequest = originalRequest.newBuilder()
.addHeader(AUTHORIZATION, "$BEARER $newAccessToken")
.build()
Expand Down

0 comments on commit 2a11fbc

Please sign in to comment.