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

πŸ”€ :: (#76) write role save logic #77

Merged
merged 6 commits into from
Sep 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ interface LocalDataSource {
expiresAt: String
)

suspend fun saveRole(roles: String)

fun getAccessToken(): Flow<String>

fun getRefreshToken(): Flow<String>

fun getExpiresAt(): Flow<String>

fun getRole(): Flow<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class LocalDataSourceImpl @Inject constructor(
private val ACCESS_TOKEN = stringPreferencesKey("access_token")
private val REFRESH_TOKEN = stringPreferencesKey("refresh_token")
private val EXPIRES_AT = stringPreferencesKey("expires_at")
private val ROLE = stringPreferencesKey("roles")
}

override suspend fun saveToken(accessToken: String, refreshToken: String, expiresAt: String) {
Expand All @@ -30,9 +31,17 @@ class LocalDataSourceImpl @Inject constructor(
}
}

override suspend fun saveRole(roles: String) {
context.dataStore.edit {
it[ROLE] = roles
}
}

override fun getAccessToken(): Flow<String> = context.dataStore.data.map { it[ACCESS_TOKEN] ?: "" }

override fun getRefreshToken(): Flow<String> = context.dataStore.data.map { it[REFRESH_TOKEN] ?: "" }

override fun getExpiresAt(): Flow<String> = context.dataStore.data.map { it[EXPIRES_AT] ?: "" }

override fun getRole(): Flow<String> = context.dataStore.data.map { it[ROLE] ?: "" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ class AuthInterceptor @Inject constructor(
val token = JsonParser.parseString(response.body!!.string()) as JsonObject
runBlocking {
localDataSource.saveToken(
accessToken = token["access_token"].asString,
refreshToken = token["refresh_token"].asString,
expiresAt = token["expires_at"].asString
accessToken = token["accessToken"].asString,
refreshToken = token["refreshToken"].asString,
expiresAt = token["expiresAt"].asString
)
localDataSource.saveRole(token["roles"].asString)
}
} else throw TokenExpiredException()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ class AuthRepositoryImpl @Inject constructor(
refreshToken,
expiresAt
)

override suspend fun saveRole(roles: String) = localDataSource.saveRole(roles)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ interface AuthRepository {
refreshToken: String,
expiresAt: String
)

suspend fun saveRole(roles: String)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.msg.domain.usecase.auth

import com.msg.domain.repository.AuthRepository
import javax.inject.Inject

class SaveRoleUseCase @Inject constructor(
private val authRepository: AuthRepository
) {
suspend operator fun invoke(roles: String) = kotlin.runCatching { authRepository.saveRole(roles) }
}
Loading