diff --git a/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt b/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt new file mode 100644 index 0000000..b8aa3a0 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt @@ -0,0 +1,25 @@ +package org.sopt.and.data.interceptor + +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.runBlocking +import okhttp3.Interceptor +import okhttp3.Response +import org.sopt.and.utils.TokenManger +import javax.inject.Inject + +class AccessTokenInterceptor @Inject constructor( + private val tokenManager: TokenManger +) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val token = runBlocking { tokenManager.getAccessToken().firstOrNull() } + val request = chain.request().newBuilder().apply { + token?.let { addHeader(HEADER_AUTHORIZATION, it) } + }.build() + return chain.proceed(request) + } + + companion object { + const val HEADER_AUTHORIZATION = "token" + } +}