BearerTokenPlugin
A Ktor HttpClient plugin that attaches Bearer tokens to outgoing requests.
On each request, calls the configured BearerTokenPluginConfig.accessTokenProvider and, if a non-empty token is returned, adds an Authorization: Bearer {token} header. If no token is available and BearerTokenPluginConfig.eventsFlow is configured, emits a NoAccessTokenAvailableEvent.
This is the KMP equivalent of the Android-only AccessTokenInterceptor for OkHttp.
Example usage with a Credential:
val client = HttpClient {
install(BearerTokenPlugin) {
credential = myCredential
eventsFlow = myEventsFlow
accessTokenProvider = {
myCredential.refreshIfExpired().getOrNull()?.token?.accessToken
}
}
}Content copied to clipboard
Example usage with a fixed token:
val client = HttpClient {
install(BearerTokenPlugin) {
accessTokenProvider = { "my-access-token" }
}
}Content copied to clipboard