Last active
December 2, 2018 16:22
-
-
Save naturalwarren/9f9b3da26e684fdfcad42165a6cc9995 to your computer and use it in GitHub Desktop.
Interceptor that authorizes requests with an access token.
This file contains hidden or 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
| class AccessTokenInterceptor( | |
| private val tokenProvider: AccessTokenProvider | |
| ) : Interceptor { | |
| override fun intercept(chain: Interceptor.Chain): Response { | |
| val token = tokenProvider.token() | |
| return if (token == null) { | |
| chain.proceed(chain.request()) | |
| } else { | |
| val authenticatedRequest = chain.request() | |
| .newBuilder() | |
| .addHeader("Authorization", "Bearer $token") | |
| .build() | |
| chain.proceed(authenticatedRequest) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment