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.
- Loading branch information
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/AuthInterceptor.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 okhttp3.Interceptor | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import org.sopt.and.data.datasource.local.WaveLocalDataSource | ||
import javax.inject.Inject | ||
|
||
class AuthInterceptor @Inject constructor( | ||
private val localStorage: WaveLocalDataSource | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val originalRequest = chain.request() | ||
val authRequest = | ||
if (localStorage.isLogin) originalRequest.newAuthBuilder() else originalRequest | ||
return chain.proceed(authRequest) | ||
} | ||
|
||
private fun Request.newAuthBuilder() = | ||
this.newBuilder().addHeader(AUTHORIZATION, localStorage.accessToken).build() | ||
|
||
companion object { | ||
const val AUTHORIZATION = "token" | ||
} | ||
} |