Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

버디콘 로그인 API 연동 #50

Merged
merged 5 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<application
android:name=".BuddyConApplication"
android:allowBackup="true"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.yapp.buddycon.local

import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey

object PreferenceKeys {
val IS_FIRST_INSTALLATION = booleanPreferencesKey("IS_FIRST_INSTALLATION")
val NICKNAME = stringPreferencesKey("NICKNAME")
val ACCESS_TOKEN = stringPreferencesKey("ACCESS_TOKEN")
val REFRESH_TOKEN = stringPreferencesKey("REFRESH_TOKEN")
val ACCESS_TOKEN_EXPIRES_IN = longPreferencesKey("ACCESS_TOKEN_EXPIRES_IN")
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ fun BuddyConNavHost(
inclusive = true
}
}
},
onNavigateToGifticon = {
navHostController.navigate(GifticonDestination.Gifticon.route) {
popUpTo(StartUpDestination.Welcome.route) {
inclusive = true
}
}
}
)
}
Expand Down
1 change: 1 addition & 0 deletions core/network/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ android {
}

dependencies {
implementation project(":domain")
implementation libs.bundles.retrofit
implementation libs.hilt.android
kapt libs.hilt.compiler
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.yapp.buddycon.network.di

import com.yapp.buddycon.domain.repository.AuthRepository
import com.yapp.buddycon.domain.repository.TokenRepository
import com.yapp.buddycon.network.BuildConfig
import com.yapp.buddycon.network.di.qualifiers.BuddyConInterceptorQualifier
import com.yapp.buddycon.network.di.qualifiers.HttpLoggingInterceptorQualifier
import com.yapp.buddycon.network.interceptor.BuddyConInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object InterceptorModule {
@HttpLoggingInterceptorQualifier
@Provides
@Singleton
fun provideHttpLoggingInterceptor(): Interceptor =
HttpLoggingInterceptor().apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
}

@BuddyConInterceptorQualifier
@Provides
@Singleton
fun provideBuddyConInterceptor(
authRepository: AuthRepository,
tokenRepository: TokenRepository
): Interceptor =
BuddyConInterceptor(authRepository, tokenRepository)
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
package com.yapp.buddycon.network.di

import com.yapp.buddycon.network.di.qualifiers.BuddyConClient
import com.yapp.buddycon.network.di.qualifiers.BuddyConInterceptorQualifier
import com.yapp.buddycon.network.di.qualifiers.BuddyConRetrofit
import com.yapp.buddycon.network.di.qualifiers.HttpLoggingInterceptorQualifier
import com.yapp.buddycon.network.di.qualifiers.LoginClient
import com.yapp.buddycon.network.di.qualifiers.LoginRetrofit
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.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
private const val BUDDYCON_BASE_URL = "http://3.37.88.131:8080/"
private const val BUDDYCON_BASE_URL = "http://43.202.14.1:8080/"

@LoginClient
@Provides
@Singleton
fun provideRetrofit(
okHttpClient: OkHttpClient
fun provideLoginClient(
@HttpLoggingInterceptorQualifier httpLoggingInterceptor: Interceptor
): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build()

@LoginRetrofit
@Provides
@Singleton
fun provideLoginRetrofit(
@LoginClient okHttpClient: OkHttpClient
): Retrofit =
Retrofit.Builder()
.baseUrl(BUDDYCON_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()

@BuddyConClient
@Provides
@Singleton
fun provideOkHttpClient(): OkHttpClient =
fun provideBuddyConClient(
@HttpLoggingInterceptorQualifier httpLoggingInterceptor: Interceptor,
@BuddyConInterceptorQualifier buddyConInterceptor: Interceptor
): OkHttpClient =
OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
).build()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(buddyConInterceptor)
.build()

@BuddyConRetrofit
@Provides
@Singleton
fun provideBuddyConRetrofit(
@BuddyConClient okHttpClient: OkHttpClient
): Retrofit =
Retrofit.Builder()
.baseUrl(BUDDYCON_BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.yapp.buddycon.network.di

import com.yapp.buddycon.network.di.qualifiers.BuddyConRetrofit
import com.yapp.buddycon.network.di.qualifiers.LoginRetrofit
import com.yapp.buddycon.network.service.auth.AuthService
import com.yapp.buddycon.network.service.gifticon.GiftiConService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -16,6 +19,15 @@ object ServiceModule {
@Provides
@Singleton
fun provideAuthService(
retrofit: Retrofit
): AuthService = retrofit.create()
@LoginRetrofit retrofit: Retrofit
): AuthService =
retrofit.create()

/** api service */
@Provides
@Singleton
fun provideGiftiConService(
@BuddyConRetrofit retrofit: Retrofit
): GiftiConService =
retrofit.create()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yapp.buddycon.data.di.qualifiers
package com.yapp.buddycon.network.di.qualifiers

import javax.inject.Qualifier

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yapp.buddycon.data.di.qualifiers
package com.yapp.buddycon.network.di.qualifiers

import javax.inject.Qualifier

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yapp.buddycon.data.di.qualifiers
package com.yapp.buddycon.network.di.qualifiers

import javax.inject.Qualifier

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.yapp.buddycon.network.interceptor

import com.yapp.buddycon.domain.repository.AuthRepository
import com.yapp.buddycon.domain.repository.TokenRepository
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Inject

class BuddyConInterceptor @Inject constructor(
private val authRepository: AuthRepository,
private val tokenRepository: TokenRepository
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var accessToken = runBlocking { tokenRepository.getAccessToken().first() }
val refreshToken = runBlocking { tokenRepository.getRefreshToken().first() }
val accessTokenExpiresIn = runBlocking { tokenRepository.getAccessTokenExpiresIn().first() }
val currentTime = System.currentTimeMillis()

if (accessTokenExpiresIn < currentTime) {
try {
val token = runBlocking {
authRepository.fetchReissue(
accessToken = accessToken,
refreshToken = refreshToken
).first()
}
accessToken = token.accessToken
runBlocking {
tokenRepository.saveAccessToken(accessToken)
tokenRepository.saveRefreshToken(refreshToken)
tokenRepository.saveAccessTokenExpiresIn(accessTokenExpiresIn)
}
} catch (e: Exception) {
}
}

val newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer $accessToken")
.build()
return chain.proceed(newRequest)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.yapp.buddycon.network.service.auth

import com.yapp.buddycon.network.service.auth.request.LoginRequest
import com.yapp.buddycon.network.service.auth.request.ReissueRequest
import com.yapp.buddycon.network.service.auth.response.LoginResponse
import retrofit2.http.Body
import retrofit2.http.POST

Expand All @@ -9,5 +11,10 @@ interface AuthService {
@POST("api/v1/auth/login")
suspend fun fetchLogin(
@Body loginRequest: LoginRequest
)
): LoginResponse

@POST("api/v1/auth/reissue")
suspend fun fetchReissue(
@Body reissueRequest: ReissueRequest
): LoginResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.yapp.buddycon.network.service.auth.request
data class LoginRequest(
val oauthAccessToken: String,
val nickname: String,
val email: String?,
val gender: String?,
val age: String?
val email: String,
val gender: String,
val age: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.yapp.buddycon.network.service.auth.request

data class ReissueRequest(
val accessToken: String,
val refreshToken: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.yapp.buddycon.network.service.auth.response

import com.yapp.buddycon.domain.model.auth.LoginModel

data class LoginResponse(
val status: Int,
val message: String,
val body: LoginResponseBody
)

data class LoginResponseBody(
val accessToken: String,
val refreshToken: String,
val accessTokenExpiresIn: Long
) {
fun toModel() = LoginModel(
accessToken = accessToken,
refreshToken = refreshToken,
accessTokenExpiresIn = accessTokenExpiresIn
)
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.yapp.buddycon.data.api
package com.yapp.buddycon.network.service.gifticon

import com.yapp.buddycon.data.model.response.AvailableGifticonResponse
import com.yapp.buddycon.network.service.gifticon.response.AvailableGifticonResponse
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Query

interface GiftiConService {
@GET("api/v1/gifticons/available")
suspend fun requestGiftiConDetail(
@Header("Authorization") token: String = "Bearer " +
"eyJhbGciOiJIUzUxMiJ9.eyJpZCI6MiwiaWF0IjoxNzAyNzg5NDc2LCJleHAiOjE3MDg4Mzc0NzZ9." +
"8YPrIlLexzGiqHwE1T_n2E-hCYbsNqJA5kUPWwgD0H8GmrGGsMgexme4NnNzBgsiHWG2uGtDLZL9fDCdiyZNUw",
@Query("pageNumber") pageNumber: Int, // page
@Query("gifticonStoreCategory") gifticonStoreCategory: String?, // 기프티콘 가게 카테고리
@Query("rowCount") rowCount: Int = 20 // page 당 요청 데이터 개수
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yapp.buddycon.data.model.response
package com.yapp.buddycon.network.service.gifticon.response

import com.google.gson.annotations.SerializedName
import com.yapp.buddycon.domain.model.gifticon.AvailableGifticon
Expand Down
23 changes: 0 additions & 23 deletions data/src/main/java/com/yapp/buddycon/data/di/InterceptorModule.kt

This file was deleted.

Loading
Loading