Skip to content

Commit

Permalink
[feat] : #8 로그인 응답으로 받는 토큰 DataStore에 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
1971123-seongmin committed Nov 12, 2024
1 parent f6be2c0 commit b60260f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/org/sopt/and/GlobalApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package org.sopt.and

import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import org.sopt.and.utils.TokenManger
import org.sopt.and.utils.TokenManager

@HiltAndroidApp
class GlobalApplication: Application() {
private lateinit var dataStore: TokenManger
private lateinit var dataStore: TokenManager

companion object {
lateinit var globalApplication: GlobalApplication
Expand All @@ -17,9 +17,9 @@ class GlobalApplication: Application() {
super.onCreate()

globalApplication = this
dataStore = TokenManger(this)
dataStore = TokenManager(this)
}

fun getDataStore(): TokenManger = dataStore
fun getDataStore(): TokenManager = dataStore

}
4 changes: 2 additions & 2 deletions app/src/main/java/org/sopt/and/data/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.sopt.and.BuildConfig
import org.sopt.and.data.interceptor.AccessTokenInterceptor
import org.sopt.and.utils.TokenManger
import org.sopt.and.utils.TokenManager
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit
import javax.inject.Qualifier
Expand Down Expand Up @@ -51,7 +51,7 @@ object NetworkModule {
@Provides
@Singleton
@Token
fun provideTokenInterceptor(tokenManager: TokenManger): AccessTokenInterceptor {
fun provideTokenInterceptor(tokenManager: TokenManager): AccessTokenInterceptor {
return AccessTokenInterceptor(tokenManager)
}

Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.sopt.and.data.di

import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import org.sopt.and.data.remote.source.AuthDataSource
import org.sopt.and.data.remote.source.AuthRepositoryImpl
import org.sopt.and.domain.repository.AuthRepository
import org.sopt.and.utils.TokenManager
import javax.inject.Singleton

@Module
Expand All @@ -15,7 +18,16 @@ object RepositoryModule {

@Provides
@Singleton
fun provideAuthRepository(authDataSource: AuthDataSource): AuthRepository =
AuthRepositoryImpl(authDataSource)
fun provideAuthRepository(
authDataSource: AuthDataSource,
tokenManager: TokenManager
): AuthRepository =
AuthRepositoryImpl(authDataSource, tokenManager)

@Provides
@Singleton
fun provideTokenManager(@ApplicationContext context: Context): TokenManager {
return TokenManager(context)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.runBlocking
import okhttp3.Interceptor
import okhttp3.Response
import org.sopt.and.utils.TokenManger
import org.sopt.and.utils.TokenManager
import javax.inject.Inject

class AccessTokenInterceptor @Inject constructor(
private val tokenManager: TokenManger
private val tokenManager: TokenManager
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import org.sopt.and.domain.model.user.RegisterUserEntity
import org.sopt.and.domain.model.user.UserIdEntity
import org.sopt.and.domain.model.user.UserTokenEntity
import org.sopt.and.domain.repository.AuthRepository
import org.sopt.and.utils.TokenManager
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val authDataSource: AuthDataSource,
private val tokenManager: TokenManager
) : AuthRepository {

override suspend fun registerUser(
Expand All @@ -24,7 +26,10 @@ class AuthRepositoryImpl @Inject constructor(
runCatching {
val requestDto = UserMapper.mapperToUserLoginRequestDto(loginUserEntity)
val response = authDataSource.loginUser(requestDto)
UserMapper.mapperToTUserTokenEntity(response.result)
val tokenEntity = UserMapper.mapperToTUserTokenEntity(response.result)

tokenManager.saveToken(tokenEntity.token)
tokenEntity
}

}
Original file line number Diff line number Diff line change
@@ -1,63 +1,37 @@
package org.sopt.and.utils

import android.content.Context
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

class TokenManger @Inject constructor(
class TokenManager @Inject constructor(
@ApplicationContext private val context: Context
) {

companion object {
private val Context.infoDataStore: DataStore<Preferences> by preferencesDataStore(name = "info")
private val ACCESS_TOKEN_KEY = stringPreferencesKey("access_token")
val EMAIL = stringPreferencesKey("user_email")
val PWD = stringPreferencesKey("user_pwd")
}

fun saveToken(token: String) = runBlocking {
context.infoDataStore.edit { prefs ->
prefs[ACCESS_TOKEN_KEY] = token
}
Log.d("토큰", "토큰 : $token")
}

fun getAccessToken(): Flow<String?> {
return context.infoDataStore.data.map { prefs ->
prefs[ACCESS_TOKEN_KEY]
}
}

suspend fun saveEmail(email: String) {
context.infoDataStore.edit {
it[EMAIL] = email
}
}

suspend fun savePwd(pwd: String) {
context.infoDataStore.edit {
it[PWD] = pwd
}
}

fun getEmail(): Flow<String?> {
return context.infoDataStore.data.map { prefs ->
prefs[EMAIL]
}
}

fun getPwd(): Flow<String?> {
return context.infoDataStore.data.map { prefs ->
prefs[PWD]
}
}

}

0 comments on commit b60260f

Please sign in to comment.