Skip to content

Commit

Permalink
[FEAT/#151] 토큰 재발급 UseCase 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Nov 13, 2024
1 parent 4a2ccae commit 90cc5b2
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 81 deletions.
100 changes: 47 additions & 53 deletions app/src/main/java/co/orange/ddanzi/di/interceptor/AuthInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import android.os.Looper
import co.orange.auth.login.LoginActivity
import co.orange.core.amplitude.AmplitudeManager
import co.orange.core.extension.toast
import co.orange.domain.entity.request.ReissueRequestModel
import co.orange.domain.repository.AuthRepository
import co.orange.domain.repository.UserRepository
import co.orange.domain.usecase.AuthReissueTokenUseCase
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
Expand All @@ -19,68 +18,63 @@ import timber.log.Timber
import javax.inject.Inject

class AuthInterceptor
@Inject
constructor(
private val authRepository: AuthRepository,
private val userRepository: UserRepository,
@ApplicationContext private val context: Context,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()
@Inject
constructor(
private val authReissueTokenUseCase: AuthReissueTokenUseCase,
private val userRepository: UserRepository,
@ApplicationContext private val context: Context,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest = chain.request()

Timber.tag("okhttp").d("ACCESS TOKEN : ${userRepository.getAccessToken()}")
Timber.tag("okhttp").d("ACCESS TOKEN : ${userRepository.getAccessToken()}")

val authRequest =
if (userRepository.getAccessToken().isNotBlank()) {
originalRequest.newBuilder().newAuthBuilder().build()
} else {
originalRequest
}
val authRequest =
if (userRepository.getAccessToken().isNotBlank()) {
originalRequest.newBuilder().newAuthBuilder().build()
} else {
originalRequest
}

val response = chain.proceed(authRequest)
val response = chain.proceed(authRequest)

if (response.code == CODE_TOKEN_EXPIRED) {
try {
runBlocking {
authRepository.postReissueTokens(
ReissueRequestModel(userRepository.getRefreshToken()),
)
}.onSuccess { data ->
userRepository.setTokens(
data.accessToken,
data.refreshToken,
)
response.close()
if (response.code == CODE_TOKEN_EXPIRED) {
try {
runBlocking {
authReissueTokenUseCase()
}.onSuccess {
response.close()

val newRequest =
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder()
.build()
return chain.proceed(newRequest)
}
} catch (t: Throwable) {
Timber.tag("okhttp").d(t)
val newRequest =
authRequest.newBuilder().removeHeader(AUTHORIZATION).newAuthBuilder()
.build()
return chain.proceed(newRequest)
}
} catch (t: Throwable) {
Timber.tag("okhttp").d(t)
}

userRepository.clearInfo()
userRepository.clearInfo()

Handler(Looper.getMainLooper()).post {
AmplitudeManager.trackEvent("view_sign_up", mapOf("sign_up_from" to "token"))
context.toast(TOKEN_EXPIRED_ERROR)
Intent(context, LoginActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(this)
}
Handler(Looper.getMainLooper()).post {
AmplitudeManager.trackEvent("view_sign_up", mapOf("sign_up_from" to "token"))
context.toast(TOKEN_EXPIRED_ERROR)
Intent(context, LoginActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
context.startActivity(this)
}
}
return response
}
return response
}

private fun Request.Builder.newAuthBuilder() = this.addHeader(AUTHORIZATION, "$BEARER ${userRepository.getAccessToken()}")
private fun Request.Builder.newAuthBuilder() =
this.addHeader(AUTHORIZATION, "$BEARER ${userRepository.getAccessToken()}")

companion object {
private const val CODE_TOKEN_EXPIRED = 401
private const val TOKEN_EXPIRED_ERROR = "토큰이 만료되었어요\n다시 로그인 해주세요"
private const val BEARER = "Bearer"
private const val AUTHORIZATION = "Authorization"
}
companion object {
private const val CODE_TOKEN_EXPIRED = 401
private const val TOKEN_EXPIRED_ERROR = "토큰이 만료되었어요\n다시 로그인 해주세요"
private const val BEARER = "Bearer"
private const val AUTHORIZATION = "Authorization"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ class AuthRepositoryImpl
constructor(
private val authDataSource: AuthDataSource,
) : AuthRepository {
override suspend fun postReissueTokens(request: ReissueRequestModel): Result<ReissueTokenModel> =
runCatching {
authDataSource.postReissueTokens(
request.toDto(),
).data.toModel()
}
override suspend fun postReissueTokens(request: ReissueRequestModel): ReissueTokenModel =
authDataSource.postReissueTokens(
request.toDto(),
).data.toModel()

override suspend fun postOauthDataToGetToken(request: AuthRequestModel): AuthTokenModel =
authDataSource.postOauthDataToGetToken(request.toDto()).data.toModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import co.orange.domain.entity.response.ReissueTokenModel
import co.orange.domain.entity.response.SignUpModel

interface AuthRepository {
suspend fun postReissueTokens(request: ReissueRequestModel): Result<ReissueTokenModel>
suspend fun postReissueTokens(request: ReissueRequestModel): ReissueTokenModel

suspend fun postOauthDataToGetToken(request: AuthRequestModel): AuthTokenModel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AuthChangeTokenAndSaveUseCase @Inject constructor(
private val userRepository: UserRepository
) {
suspend operator fun invoke(accessToken: String, fcmToken: String) = runCatching {
val authTokenModel = authRepository.postOauthDataToGetToken(
val response = authRepository.postOauthDataToGetToken(
AuthRequestModel(
accessToken,
KAKAO,
Expand All @@ -19,9 +19,9 @@ class AuthChangeTokenAndSaveUseCase @Inject constructor(
fcmToken,
),
)
userRepository.setTokens(authTokenModel.accesstoken, authTokenModel.refreshtoken)
userRepository.setUserStatus(authTokenModel.status)
return@runCatching authTokenModel.status
userRepository.setTokens(response.accesstoken, response.refreshtoken)
userRepository.setUserStatus(response.status)
return@runCatching response.status
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package co.orange.domain.usecase

import co.orange.domain.entity.request.ReissueRequestModel
import co.orange.domain.repository.AuthRepository
import co.orange.domain.repository.UserRepository
import javax.inject.Inject

class AuthReissueTokenUseCase @Inject constructor(
private val authRepository: AuthRepository,
private val userRepository: UserRepository,
) {
suspend operator fun invoke() = runCatching {
val response = authRepository.postReissueTokens(
ReissueRequestModel(userRepository.getRefreshToken()),
)
userRepository.setTokens(
response.accessToken,
response.refreshToken,
)
return@runCatching response
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class AuthSignUpAndSaveStatusUseCase @Inject constructor(
private val userRepository: UserRepository,
) {
suspend operator fun invoke(
response: IamportCertificationModel,
iamportCertificationModel: IamportCertificationModel,
isTermMarketingSelected: Boolean?
) = runCatching {
val request = SignUpRequestModel(
name = response.name.orEmpty(),
phone = response.phone.orEmpty(),
birth = response.birthday.orEmpty(),
sex = response.gender?.uppercase().orEmpty(),
name = iamportCertificationModel.name.orEmpty(),
phone = iamportCertificationModel.phone.orEmpty(),
birth = iamportCertificationModel.birthday.orEmpty(),
sex = iamportCertificationModel.gender?.uppercase().orEmpty(),
isAgreedMarketingTerm = isTermMarketingSelected ?: false,
ci = response.uniqueKey.orEmpty(),
ci = iamportCertificationModel.uniqueKey.orEmpty(),
)
val signUpModel = authRepository.postToSignUp(userRepository.getAccessToken(), request)
userRepository.setUserStatus(signUpModel.status)
return@runCatching signUpModel
val response = authRepository.postToSignUp(userRepository.getAccessToken(), request)
userRepository.setUserStatus(response.status)
return@runCatching response
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class IamportGetDataAndSaveUseCase @Inject constructor(
private val userRepository: UserRepository,
) {
suspend operator fun invoke(accessToken: String, certificatedUid: String) = runCatching {
val authCertificationModel: IamportCertificationModel? =
val response: IamportCertificationModel? =
iamportRepository.getIamportCertificationData(accessToken, certificatedUid)
if (authCertificationModel != null) {
if (response != null) {
userRepository.setUserInfo(
userName = authCertificationModel.name.orEmpty(),
userPhone = authCertificationModel.phone?.toPhoneFrom().orEmpty(),
userName = response.name.orEmpty(),
userPhone = response.phone?.toPhoneFrom().orEmpty(),
)
return@runCatching authCertificationModel
return@runCatching response
} else {
throw IllegalArgumentException()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class IamportGetTokenUseCase @Inject constructor(
private val iamportRepository: IamportRepository,
) {
suspend operator fun invoke(certificatedUid: String) = runCatching {
val authTokenModel: IamportTokenModel? = iamportRepository.postToGetIamportToken()
if (certificatedUid.isNotBlank() && authTokenModel != null) {
return@runCatching authTokenModel.accessToken
val response: IamportTokenModel? = iamportRepository.postToGetIamportToken()
if (certificatedUid.isNotBlank() && response != null) {
return@runCatching response.accessToken
} else {
throw IllegalArgumentException()
}
Expand Down

0 comments on commit 90cc5b2

Please sign in to comment.