feat: Store user id with the refresh token

This commit is contained in:
Peter Vacho 2024-12-30 01:48:12 +01:00
parent 1ba9a2b0ba
commit bd43bc7958
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 10 additions and 4 deletions

View file

@ -32,7 +32,7 @@ class AuthRepository(private val tokenManager: TokenManager) {
try {
val response = RetrofitClient.authService.login(username, password)
tokenManager.storeAccessToken(response.access_token, response.expires_in)
tokenManager.storeRefreshToken(response.refresh_token, response.refresh_token_expires_in)
tokenManager.storeRefreshToken(response.refresh_token, response.refresh_token_expires_in, response.user_id)
Log.i("API", "Login successful")
LoginResult.Success
} catch (e: HttpException) {
@ -90,7 +90,7 @@ class AuthRepository(private val tokenManager: TokenManager) {
// Validate the access token by fetching session info
suspend fun validateAccessToken(): SessionResponse? {
return withContext(Dispatchers.IO) {
val token = tokenManager.accessToken;
val token = tokenManager.accessToken
if (token == null) {
Log.w("API", "Access token validation failed, token not stored")
@ -114,7 +114,7 @@ class AuthRepository(private val tokenManager: TokenManager) {
// Validate the refresh token by fetching session info
suspend fun validateRefreshToken(): SessionResponse? {
return withContext(Dispatchers.IO) {
val token = tokenManager.refreshToken;
val token = tokenManager.refreshToken
if (token == null) {
Log.w("API", "Refresh token validation failed, token not stored")

View file

@ -15,6 +15,10 @@ class TokenManager(context: Context) {
get() = prefs.getString("refresh_token", null)
private set(value) = prefs.edit().putString("refresh_token", value).apply()
var userId: String?
get() = prefs.getString("user_id", null)
private set(value) = prefs.edit().putString("user_id", value).apply()
var accessTokenExpiresAt: Long
get() = prefs.getLong("access_token_expires_at", 0L)
private set(value) = prefs.edit().putLong("access_token_expires_at", value).apply()
@ -44,9 +48,10 @@ class TokenManager(context: Context) {
this.accessTokenExpiresAt = System.currentTimeMillis() / 1000 + expiresIn
}
fun storeRefreshToken(refreshToken: String, expiresIn: Int) {
fun storeRefreshToken(refreshToken: String, expiresIn: Int, userId: String) {
this.refreshToken = refreshToken
this.refreshTokenExpiresAt = System.currentTimeMillis() / 1000 + expiresIn
this.userId = userId
}
fun clearAccessToken() {
@ -58,5 +63,6 @@ class TokenManager(context: Context) {
clearAccessToken()
refreshToken = null
refreshTokenExpiresAt = 0L
userId = null
}
}