diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/DataSourceModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt similarity index 94% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/DataSourceModule.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt index d9786a3..375bc93 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/DataSourceModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/DataSourceModule.kt @@ -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 diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/Extension.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/Extension.kt similarity index 79% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/Extension.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/Extension.kt index 670021d..2a5997d 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/Extension.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/Extension.kt @@ -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("]") \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/Qualifier.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/Qualifier.kt similarity index 61% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/Qualifier.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/Qualifier.kt index 6beb496..f1870b1 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/Qualifier.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/Qualifier.kt @@ -1,4 +1,4 @@ -package com.hackathon.alddeul_babsang.app +package com.hackathon.alddeul_babsang.app.di import javax.inject.Qualifier @@ -6,6 +6,10 @@ import javax.inject.Qualifier @Retention(AnnotationRetention.BINARY) annotation class AlddeulRetrofit +@Qualifier +@Retention(AnnotationRetention.BINARY) +annotation class AccessToken + @Qualifier @Retention(AnnotationRetention.BINARY) annotation class UserPreferences diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/RepositoryModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt similarity index 94% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/RepositoryModule.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt index 67b9be6..9e11386 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/RepositoryModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RepositoryModule.kt @@ -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 diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/RetrofitModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RetrofitModule.kt similarity index 80% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/RetrofitModule.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/RetrofitModule.kt index 455714f..3811a20 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/RetrofitModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/RetrofitModule.kt @@ -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 @@ -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 { diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/ServiceModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt similarity index 91% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/ServiceModule.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt index 308511a..952ed33 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/ServiceModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/ServiceModule.kt @@ -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 diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/UserPreferencesModule.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/UserPreferencesModule.kt similarity index 94% rename from app/src/main/java/com/hackathon/alddeul_babsang/app/UserPreferencesModule.kt rename to app/src/main/java/com/hackathon/alddeul_babsang/app/di/UserPreferencesModule.kt index 9e9c3f2..18ca339 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/app/UserPreferencesModule.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/di/UserPreferencesModule.kt @@ -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 diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/app/interceptor/TokenInterceptor.kt b/app/src/main/java/com/hackathon/alddeul_babsang/app/interceptor/TokenInterceptor.kt new file mode 100644 index 0000000..3dc4f5d --- /dev/null +++ b/app/src/main/java/com/hackathon/alddeul_babsang/app/interceptor/TokenInterceptor.kt @@ -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) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/UserPreferencesDataSourceImpl.kt b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/UserPreferencesDataSourceImpl.kt index 73342ce..65bcce9 100644 --- a/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/UserPreferencesDataSourceImpl.kt +++ b/app/src/main/java/com/hackathon/alddeul_babsang/data/datasourceimpl/UserPreferencesDataSourceImpl.kt @@ -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