Skip to content

Commit

Permalink
#20 [FEAT] 토큰 인터셉터 모듈 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeulzzang committed Nov 7, 2024
1 parent 2ea227b commit a917dd3
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.data.datasource.ExampleDataSource
import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

fun String?.isJsonObject(): Boolean = this?.startsWith("{") == true && this.endsWith("}")
fun String?.isJsonArray(): Boolean = this?.startsWith("[") == true && this.endsWith("]")
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AlddeulRetrofit

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class AccessToken

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UserPreferences
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.data.repositoryimpl.ExampleRepositoryImpl
import com.hackathon.alddeul_babsang.data.repositoryimpl.UserPreferencesRepositoryImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.hackathon.alddeul_babsang.BuildConfig.BASE_URL
import com.hackathon.alddeul_babsang.app.interceptor.TokenInterceptor
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.serialization.json.Json
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -23,13 +25,20 @@ object RetrofitModule {
@Provides
@Singleton
fun provideOkHttpClient(
loggingInterceptor: HttpLoggingInterceptor
loggingInterceptor: HttpLoggingInterceptor,
@AccessToken tokenInterceptor: Interceptor,
): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(tokenInterceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()

@Provides
@Singleton
@AccessToken
fun provideAuthInterceptor(interceptor: TokenInterceptor): Interceptor = interceptor

@Provides
@Singleton
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import com.sopt.data.service.ExampleApiService
import dagger.Module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hackathon.alddeul_babsang.app
package com.hackathon.alddeul_babsang.app.di

import android.content.Context
import androidx.datastore.core.DataStore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hackathon.alddeul_babsang.app.interceptor

import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import timber.log.Timber
import javax.inject.Inject

class TokenInterceptor @Inject constructor(
private val userPreferencesDataSource: UserPreferencesDataSource
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response = runBlocking {
val request = chain.request()
val url = request.url.toString()

// 특정 URL 패턴에 대해서는 토큰을 추가하지 않음
if (url.contains("/api/v1/auth/login") ||
url.contains("api/v1/signup") ||
url.contains("api/v1/member")) {
// 로그인 요청 등 토큰이 필요 없는 요청의 경우
return@runBlocking chain.proceed(request)
}

// 토큰이 필요한 요청의 경우
val accessToken = userPreferencesDataSource.getUserAccessToken().first()
val newRequest = request.newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()

Timber.tag("interceptor").d("accessToken $accessToken")
chain.proceed(newRequest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.hackathon.alddeul_babsang.app.UserPreferences
import com.hackathon.alddeul_babsang.app.di.UserPreferences
import com.hackathon.alddeul_babsang.data.datasource.UserPreferencesDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
Expand Down

0 comments on commit a917dd3

Please sign in to comment.