Skip to content

Commit

Permalink
feat: token 로컬 저장 추가 #52
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Aug 11, 2024
1 parent c762733 commit 31bb419
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.easyhz.noffice.core.datastore.datasource.auth


interface AuthLocalDataSource {
suspend fun getAccessToken(): Result<String>
suspend fun getRefreshToken(): Result<String>
suspend fun deleteToken()
suspend fun updateAccessToken(access: String)
suspend fun updateTokens(access: String, refresh: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.easyhz.noffice.core.datastore.datasource.auth

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.easyhz.noffice.core.common.di.Dispatcher
import com.easyhz.noffice.core.common.di.NofficeDispatchers.IO
import com.easyhz.noffice.core.datastore.util.AuthKey
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import javax.inject.Inject

class AuthLocalDataSourceImpl @Inject constructor(
@Dispatcher(IO) private val dispatcher: CoroutineDispatcher,
private val dataStore: DataStore<Preferences>
): AuthLocalDataSource {
private val accessToken = stringPreferencesKey(AuthKey.ACCESS_TOKEN.key)
private val refreshToken = stringPreferencesKey(AuthKey.REFRESH_TOKEN.key)

override suspend fun getAccessToken(): Result<String> = withContext(dispatcher) {
val preferences = dataStore.data.first()
return@withContext preferences[accessToken]?.let {
Result.success(it)
} ?: Result.failure(generateNullException(AuthKey.ACCESS_TOKEN))
}

override suspend fun getRefreshToken(): Result<String> = withContext(dispatcher) {
val preferences = dataStore.data.first()
return@withContext preferences[refreshToken]?.let {
Result.success(it)
} ?: Result.failure(generateNullException(AuthKey.REFRESH_TOKEN))
}

override suspend fun deleteToken() {
dataStore.edit { preferences ->
preferences.remove(accessToken)
preferences.remove(refreshToken)
}
}

override suspend fun updateAccessToken(access: String) {
dataStore.edit { preferences ->
preferences[accessToken] = access
}
}

override suspend fun updateTokens(access: String, refresh: String) {
dataStore.edit { preferences ->
preferences[accessToken] = access
preferences[refreshToken] = refresh
}
}

private fun generateNullException(authKey: AuthKey): Exception {
return Exception("${authKey.key} is null")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.easyhz.noffice.core.datastore.di

import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSource
import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
interface DataSourceModule {
@Binds
fun bindAuthLocalDataSource(
authLocalDataSourceImpl: AuthLocalDataSourceImpl
): AuthLocalDataSource
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.easyhz.noffice.core.datastore.di

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStoreFile
import androidx.datastore.preferences.core.Preferences
import com.easyhz.noffice.core.datastore.util.authDataStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
abstract class DataStoreModule {
companion object {

@Singleton
@Provides
fun provideUserDataStorePreferences(
@ApplicationContext context: Context
): DataStore<Preferences> =
context.authDataStore
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.easyhz.noffice.core.datastore.util

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore

val Context.authDataStore: DataStore<Preferences> by preferencesDataStore(
name = "noffice-auth"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.easyhz.noffice.core.datastore.util

internal enum class AuthKey(
val key: String
) {
ACCESS_TOKEN(
key = "ACCESS_TOKEN"
), REFRESH_TOKEN(
key = "REFRESH_TOKEN"
)
}

0 comments on commit 31bb419

Please sign in to comment.