-
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.
Merge pull request #21 from ryan-conway/feature/auth_interceptor
Feature/auth interceptor
- Loading branch information
Showing
15 changed files
with
171 additions
and
88 deletions.
There are no files selected for viewing
50 changes: 42 additions & 8 deletions
50
app/src/main/java/com/example/nimblesurveys/di/ApiModule.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 |
---|---|---|
@@ -1,36 +1,70 @@ | ||
package com.example.nimblesurveys.di | ||
|
||
import com.example.nimblesurveys.BuildConfig | ||
import com.example.nimblesurveys.data.api.auth.AuthInterceptor | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ApiModule { | ||
|
||
@Provides | ||
fun provideRetrofit(): Retrofit { | ||
fun provideLoggingInterceptor(): Interceptor { | ||
return HttpLoggingInterceptor().apply { | ||
level = if (BuildConfig.DEBUG) { | ||
HttpLoggingInterceptor.Level.BODY | ||
} else { | ||
HttpLoggingInterceptor.Level.NONE | ||
} | ||
} | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAuthInterceptor(): AuthInterceptor { | ||
return AuthInterceptor() | ||
} | ||
|
||
@Provides | ||
fun provideOkHttpClient( | ||
loggingInterceptor: Interceptor, | ||
authInterceptor: AuthInterceptor, | ||
): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
.addInterceptor(loggingInterceptor) | ||
.addInterceptor(authInterceptor) | ||
.build() | ||
} | ||
|
||
@Provides | ||
fun provideConverterFactory(): Converter.Factory { | ||
val moshi = Moshi.Builder() | ||
.addLast(KotlinJsonAdapterFactory()) | ||
.build() | ||
val interceptor = HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
val okHttpClient = OkHttpClient.Builder() | ||
.addInterceptor(interceptor) | ||
.build() | ||
return MoshiConverterFactory.create(moshi) | ||
} | ||
|
||
@Provides | ||
fun provideRetrofit( | ||
okHttpClient: OkHttpClient, | ||
converterFactory: Converter.Factory | ||
): Retrofit { | ||
return Retrofit.Builder() | ||
.baseUrl(BuildConfig.API_BASE_URL) | ||
.client(okHttpClient) | ||
.addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
.addConverterFactory(converterFactory) | ||
.build() | ||
} | ||
} |
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
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
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
31 changes: 31 additions & 0 deletions
31
data/src/main/java/com/example/nimblesurveys/data/api/auth/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,31 @@ | ||
package com.example.nimblesurveys.data.api.auth | ||
|
||
import com.example.nimblesurveys.domain.exception.UnauthorizedException | ||
import com.example.nimblesurveys.domain.model.Token | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class AuthInterceptor: Interceptor { | ||
|
||
private var token: Token? = null | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val hasAuth = chain.request().header("No-Authentication") == null | ||
val requestBuilder = chain.request().newBuilder() | ||
if (hasAuth) { | ||
val token = token | ||
if (token == null) { | ||
throw UnauthorizedException() | ||
} else { | ||
val authorization = "${token.tokenType} ${token.accessToken}" | ||
requestBuilder.addHeader("Authorization", authorization) | ||
} | ||
} | ||
|
||
return chain.proceed(requestBuilder.build()) | ||
} | ||
|
||
fun setAccessToken(token: Token) { | ||
this.token = token | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
data/src/main/java/com/example/nimblesurveys/data/api/survey/SurveyApiService.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
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
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
Oops, something went wrong.