refactor: Add logging & improve AuthRepository
This commit is contained in:
parent
0c0839a7d8
commit
32dce50a0a
|
@ -1,5 +1,6 @@
|
|||
package com.p_vacho.neat_calendar.auth
|
||||
|
||||
import android.util.Log
|
||||
import com.p_vacho.neat_calendar.api.RetrofitClient
|
||||
import com.p_vacho.neat_calendar.api.models.LoginRequest
|
||||
import com.p_vacho.neat_calendar.api.models.SessionResponse
|
||||
|
@ -15,20 +16,58 @@ class AuthRepository(private val tokenManager: TokenManager) {
|
|||
val response = RetrofitClient.authService.login(LoginRequest(username, password))
|
||||
tokenManager.storeAccessToken(response.access_token, response.expires_in)
|
||||
tokenManager.storeRefreshToken(response.refresh_token, response.refresh_token_expires_in)
|
||||
Log.i("API", "Login successful")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.w("API", "Login request failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate token by fetching session info
|
||||
suspend fun validateToken(): SessionResponse? {
|
||||
// Validate the access token by fetching session info
|
||||
suspend fun validateAccessToken(): SessionResponse? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val token = tokenManager.accessToken ?: return@withContext null
|
||||
val token = tokenManager.accessToken;
|
||||
|
||||
if (token == null) {
|
||||
Log.w("API", "Access token validation failed, token not stored")
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
if (tokenManager.isAccessTokenExpired()) {
|
||||
Log.w("API", "Access token validation failed, token expired")
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
try {
|
||||
RetrofitClient.authService.getSession("Bearer $token")
|
||||
} catch (e: Exception) {
|
||||
Log.w("API", "Access token validation request failed", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the refresh token by fetching session info
|
||||
suspend fun validateRefreshToken(): SessionResponse? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val token = tokenManager.refreshToken;
|
||||
|
||||
if (token == null) {
|
||||
Log.w("API", "Refresh token validation failed, token not stored")
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
if (tokenManager.isRefreshTokenExpired()) {
|
||||
Log.w("API", "Refresh token validation failed, token expired")
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
try {
|
||||
RetrofitClient.authService.getSession("Bearer $token")
|
||||
} catch (e: Exception) {
|
||||
Log.w("API", "Refresh token validation request failed", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
@ -37,18 +76,26 @@ class AuthRepository(private val tokenManager: TokenManager) {
|
|||
// Refresh access token if valid refresh token exists
|
||||
suspend fun refreshAccessToken(): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val refreshToken = tokenManager.refreshToken
|
||||
|
||||
if (refreshToken == null) {
|
||||
Log.w("API", "Access token refresh failed, no refresh token stored")
|
||||
return@withContext false
|
||||
}
|
||||
|
||||
if (tokenManager.isRefreshTokenExpired()) {
|
||||
// Refresh token is expired; log out user
|
||||
logout()
|
||||
Log.w("API", "Access token refresh failed, refresh token is expired")
|
||||
tokenManager.clearTokens() // no longer valid anyways
|
||||
return@withContext false
|
||||
}
|
||||
|
||||
try {
|
||||
val refreshToken = tokenManager.refreshToken ?: return@withContext false
|
||||
val response = RetrofitClient.authService.refreshToken("Bearer $refreshToken")
|
||||
tokenManager.storeAccessToken(response.access_token, response.expires_in)
|
||||
Log.i("API", "Access token refreshed")
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.w("API", "Access token refresh request failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
@ -57,16 +104,33 @@ class AuthRepository(private val tokenManager: TokenManager) {
|
|||
// Logout user and clear tokens
|
||||
suspend fun logout(): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val refreshToken = tokenManager.refreshToken
|
||||
|
||||
if (refreshToken == null) {
|
||||
// This should never happen, the user shouldn't be able to click on logout
|
||||
// when they aren't logged in
|
||||
Log.e("API", "Logout request failed, no refresh token stored")
|
||||
return@withContext false
|
||||
}
|
||||
|
||||
if (tokenManager.isRefreshTokenExpired()) {
|
||||
Log.i("API", "Logout request ignored, refresh token already expired")
|
||||
tokenManager.clearTokens()
|
||||
return@withContext false
|
||||
}
|
||||
|
||||
try {
|
||||
val refreshToken = tokenManager.refreshToken ?: return@withContext false
|
||||
val response = RetrofitClient.authService.logout("Bearer $refreshToken")
|
||||
if (response.isSuccessful) {
|
||||
Log.i("API", "Logout successful")
|
||||
tokenManager.clearTokens()
|
||||
true
|
||||
} else {
|
||||
Log.e("API", "Logout request not successful: $response")
|
||||
false
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("API", "Logout request failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue