diff --git a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt index 9508e40ed..326c77553 100644 --- a/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt +++ b/android/2023-emmsale/app/src/main/java/com/emmsale/data/common/retrofit/AuthInterceptor.kt @@ -1,25 +1,47 @@ package com.emmsale.data.common.retrofit import android.content.Context +import android.content.Intent import com.emmsale.data.repository.interfaces.TokenRepository +import com.emmsale.presentation.ui.login.LoginActivity import dagger.hilt.EntryPoint import dagger.hilt.InstallIn import dagger.hilt.android.EntryPointAccessors import dagger.hilt.components.SingletonComponent import okhttp3.Interceptor +import okhttp3.Request import okhttp3.Response -class AuthInterceptor(context: Context) : Interceptor { +class AuthInterceptor(private val context: Context) : Interceptor { private val tokenRepository = EntryPointAccessors .fromApplication(context) .getTokenRepository() override fun intercept(chain: Interceptor.Chain): Response { val token = tokenRepository.getToken() - val newRequest = chain.request().newBuilder() - .addHeader(ACCESS_TOKEN_HEADER, ACCESS_TOKEN_FORMAT.format(token?.accessToken)) - .build() - return chain.proceed(newRequest) + val tokenAddedRequest = chain.request().putAccessToken(token?.accessToken) + + val response = chain.proceed(tokenAddedRequest) + if (response.isAccessTokenInvalid()) { + navigateToLogin() + } + return response + } + + private fun Response.isAccessTokenInvalid(): Boolean = (code == 401) + + private fun Request.putAccessToken(token: String?): Request = + putHeader(ACCESS_TOKEN_HEADER, ACCESS_TOKEN_FORMAT.format(token)) + + private fun Request.putHeader( + key: String, + value: String, + ): Request = newBuilder().addHeader(key, value).build() + + private fun navigateToLogin() { + val loginStartIntent = Intent(context, LoginActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(loginStartIntent) } @EntryPoint