diff --git a/app/src/main/java/org/sopt/and/GlobalApplication.kt b/app/src/main/java/org/sopt/and/GlobalApplication.kt index a6b5096..1878936 100644 --- a/app/src/main/java/org/sopt/and/GlobalApplication.kt +++ b/app/src/main/java/org/sopt/and/GlobalApplication.kt @@ -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 @@ -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 } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/di/NetworkModule.kt b/app/src/main/java/org/sopt/and/data/di/NetworkModule.kt index 6e031f0..9300245 100644 --- a/app/src/main/java/org/sopt/and/data/di/NetworkModule.kt +++ b/app/src/main/java/org/sopt/and/data/di/NetworkModule.kt @@ -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 @@ -51,7 +51,7 @@ object NetworkModule { @Provides @Singleton @Token - fun provideTokenInterceptor(tokenManager: TokenManger): AccessTokenInterceptor { + fun provideTokenInterceptor(tokenManager: TokenManager): AccessTokenInterceptor { return AccessTokenInterceptor(tokenManager) } diff --git a/app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt b/app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt index 0faaf17..d7b5e2f 100644 --- a/app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt +++ b/app/src/main/java/org/sopt/and/data/di/RepositoryModule.kt @@ -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 @@ -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) + } } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt b/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt index b8aa3a0..dd1c23e 100644 --- a/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt +++ b/app/src/main/java/org/sopt/and/data/interceptor/AccessTokenInterceptor.kt @@ -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 { diff --git a/app/src/main/java/org/sopt/and/data/remote/source/AuthRepositoryImpl.kt b/app/src/main/java/org/sopt/and/data/remote/source/AuthRepositoryImpl.kt index c13b2c6..d422247 100644 --- a/app/src/main/java/org/sopt/and/data/remote/source/AuthRepositoryImpl.kt +++ b/app/src/main/java/org/sopt/and/data/remote/source/AuthRepositoryImpl.kt @@ -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( @@ -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 } } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/utils/TokenManger.kt b/app/src/main/java/org/sopt/and/utils/TokenManager.kt similarity index 61% rename from app/src/main/java/org/sopt/and/utils/TokenManger.kt rename to app/src/main/java/org/sopt/and/utils/TokenManager.kt index b43aee2..561cc4d 100644 --- a/app/src/main/java/org/sopt/and/utils/TokenManger.kt +++ b/app/src/main/java/org/sopt/and/utils/TokenManager.kt @@ -1,6 +1,7 @@ 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 @@ -8,26 +9,24 @@ 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 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 { @@ -35,29 +34,4 @@ class TokenManger @Inject constructor( 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 { - return context.infoDataStore.data.map { prefs -> - prefs[EMAIL] - } - } - - fun getPwd(): Flow { - return context.infoDataStore.data.map { prefs -> - prefs[PWD] - } - } - } \ No newline at end of file