generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] : #8 AccessTokenInterceptor 추가
- Loading branch information
1 parent
0a3bc75
commit 8d2e980
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |