From 8d2e980e108920e35458af99604e3e5163d8ec36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=ABsungmin?= Date: Wed, 6 Nov 2024 18:44:14 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20:=20#8=20AccessTokenInterceptor=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/AccessTokenInterceptor.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt 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" + } +}