TokenResponse rework
This commit is contained in:
@@ -8,9 +8,15 @@ package com.homebox.lens.domain.model
|
||||
/**
|
||||
* @summary Data model representing a response from the server with an authentication token.
|
||||
* @param token A string containing a JWT or other access token.
|
||||
* @param attachmentToken A token for accessing attachments.
|
||||
* @param expiresAt The expiration date of the token.
|
||||
* @invariant `token` must not be blank.
|
||||
*/
|
||||
data class TokenResponse(val token: String) {
|
||||
data class TokenResponse(
|
||||
val token: String,
|
||||
val attachmentToken: String,
|
||||
val expiresAt: String
|
||||
) {
|
||||
init {
|
||||
require(token.isNotBlank()) { "Token cannot be blank." }
|
||||
}
|
||||
|
||||
@@ -31,17 +31,19 @@ class LoginUseCase @Inject constructor(
|
||||
"Server URL and username must not be blank."
|
||||
}
|
||||
|
||||
val loginResult: com.homebox.lens.domain.model.Result<TokenResponse> = authRepository.login(credentials)
|
||||
|
||||
return loginResult.fold(
|
||||
onSuccess = {
|
||||
authRepository.saveToken(it.token)
|
||||
com.homebox.lens.domain.model.Result.Success(Unit)
|
||||
},
|
||||
onFailure = {
|
||||
com.homebox.lens.domain.model.Result.Error(it as Exception)
|
||||
return try {
|
||||
when (val loginResult = authRepository.login(credentials)) {
|
||||
is com.homebox.lens.domain.model.Result.Success -> {
|
||||
authRepository.saveToken(loginResult.data.token)
|
||||
Result.success(Unit)
|
||||
}
|
||||
is com.homebox.lens.domain.model.Result.Error -> {
|
||||
Result.failure(loginResult.exception)
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
Result.failure(e)
|
||||
}
|
||||
}
|
||||
// [END_ENTITY: Function('invoke')]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user