fix token reset

This commit is contained in:
2026-09-15 13:23:04 +03:00
parent 9203209d17
commit 43528748d6
4 changed files with 43 additions and 27 deletions
@@ -16,6 +16,7 @@ import ru.shadowsparky.http.domain.LogoutAction
import ru.shadowsparky.http.domain.RefreshTokenAction
import ru.shadowsparky.http.domain.TokenInfo
import ru.shadowsparky.http.domain.TokenStorage
import ru.shadowsparky.http.domain.isServerFailure
import ru.shadowsparky.koin
@Module
@@ -53,18 +54,19 @@ class HttpModule {
)
}
private suspend fun RefreshTokensParams.refreshTokenInternal(tokenStorage: TokenStorage?): BearerTokens? {
private suspend fun RefreshTokensParams.refreshTokenInternal(
tokenStorage: TokenStorage?
): BearerTokens? {
val token = tokenStorage?.token?.firstOrNull()?.refresh ?: return null
return try {
val newToken = koin.getOrNull<RefreshTokenAction>()
?.refresh(this.client, token)
?.refresh(client, token)
?: return null
tokenStorage.updateToken(newToken)
newToken.toKtor()
} catch (e: Throwable) {
if (e is HttpException) {
koin.getOrNull<LogoutAction>()?.logout()
}
} catch (e: HttpException) {
if (e.isServerFailure()) throw e
koin.getOrNull<LogoutAction>()?.logout()
null
}
}
@@ -8,3 +8,7 @@ open class HttpException(val httpCode: Int, override val message: String?) : Run
class BadRequestException(msg: String) : HttpException(HttpStatusCode.BadRequest.value, msg)
open class NotFoundException(msg: String) : HttpException(HttpStatusCode.NotFound.value, msg)
fun HttpException.isServerFailure(): Boolean {
return httpCode in 500..599
}