Skip to content

Commit

Permalink
Merge pull request #77 from Team-Ampersand/feature/76_write_role_save…
Browse files Browse the repository at this point in the history
…_logic

🔀 :: (#76) write role save logic
  • Loading branch information
Cjsghkd authored Sep 19, 2023
2 parents d19ffeb + 0dd454e commit 051d45a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
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) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.msg.domain.model.auth.LoginRequestModel
import com.msg.domain.model.auth.LoginResponseModel
import com.msg.domain.usecase.auth.LoginUseCase
import com.msg.domain.usecase.auth.SaveRoleUseCase
import com.msg.domain.usecase.auth.SaveTokenUseCase
import com.msg.presentation.exception.exceptionHandling
import com.msg.presentation.viewmodel.util.Event
Expand All @@ -15,7 +16,8 @@ import javax.inject.Inject

class LoginViewModel @Inject constructor(
private val loginUseCase: LoginUseCase,
private val saveTokenUseCase: SaveTokenUseCase
private val saveTokenUseCase: SaveTokenUseCase,
private val saveRoleUseCase: SaveRoleUseCase
) : ViewModel() {

private val _loginState = MutableStateFlow<Event<LoginResponseModel>>(Event.Loading)
Expand All @@ -30,6 +32,7 @@ class LoginViewModel @Inject constructor(
refreshToken = it.refreshToken,
expiresAt = it.expiresAt
)
saveRoleUseCase(it.roles.toString())
}
.onFailure {
it.exceptionHandling(
Expand Down

0 comments on commit 051d45a

Please sign in to comment.