KDefaultTokenRefresher

class KDefaultTokenRefresher(refreshUrl: String, getRefreshToken: suspend () -> String?, onTokenRefreshed: suspend (accessToken: String, refreshToken: String) -> Unit, onFailed: suspend () -> Unit, authProvider: KAuthProvider? = null) : KTokenRefresher(source)

Default KTokenRefresher implementation provided by Kindling.

Sends a POST to refreshUrl with { "refreshToken": "<token>" } and expects a { "accessToken": "...", "refreshToken": "..." } response.

Token application on retry is delegated to authProvider — so Bearer, ApiKey, Basic, Cookie, or any custom scheme all work without extra configuration.

If your refresh endpoint lives on a different server or expects a different request/response shape, implement KTokenRefresher directly instead.

Parameters

refreshUrl

Full URL of the refresh endpoint. Can differ from KHttpConfig.baseUrl (e.g. a dedicated auth server).

getRefreshToken

Returns the current refresh token.

onTokenRefreshed

Called with (accessToken, refreshToken) on success. Persist the new tokens here.

onFailed

Called when the refresh request fails or returns non-200. Clear the session here.

authProvider

The same KAuthProvider passed to KHttpConfig.authProvider. Used to re-apply the auth header on the retried request after a successful refresh — works with any auth scheme.

Constructors

Link copied to clipboard
constructor(refreshUrl: String, getRefreshToken: suspend () -> String?, onTokenRefreshed: suspend (accessToken: String, refreshToken: String) -> Unit, onFailed: suspend () -> Unit, authProvider: KAuthProvider? = null)

Functions

Link copied to clipboard
open suspend override fun applyToken(request: HttpRequestBuilder)

Re-applies the auth credential to the retried request.

Link copied to clipboard
open suspend override fun onRefreshFailed()

Called when refresh returned false or threw an exception.

Link copied to clipboard
open suspend override fun refresh(): Boolean

Performs the token refresh.

Link copied to clipboard
open suspend override fun shouldRefresh(): Boolean

Called before attempting a refresh.