Ryan Payne
05/27/2026, 4:14 PMbearer {
cacheTokens = false
}
Doc guidance says:
> Disabling token caching is especially useful when authentication data changes frequently or must reflect the most recent state.
> https://ktor.io/docs/client-auth.html#controlling-caching-behavior
My use case also feels like a good scenario to turn off caching. I don't see a reason to double cache.Aleksei Tirman [JB]
05/28/2026, 7:35 AMRyan Payne
05/28/2026, 2:48 PMimport io.ktor.client.HttpClientConfig
import io.ktor.client.plugins.auth.Auth
import io.ktor.client.plugins.auth.providers.bearer
internal fun HttpClientConfig<*>.installAuthPlugin(/* ... */) {
install(Auth) {
bearer {
loadTokens {
// Load token from Auth0 interface...
}
refreshTokens {
// Refresh token from Auth0 interface...
}
}
}
}
Providing HttpClient to the DI container:
import dev.zacsweers.metro.AppScope
import dev.zacsweers.metro.ContributesTo
import dev.zacsweers.metro.Provides
import io.ktor.client.HttpClient
@ContributesTo(AppScope::class)
interface NetworkProvider {
@Provides
fun providesHttpClient(/* ... */): HttpClient =
HttpClient(platformEngineFactory()) {
installAuthPlugin(/* ... */)
// ...
}
}
I'm using the Auth0 Credentials Manager so getting the latest token involves calling (simplified):
try {
val credentials = manager.awaitCredentials()
println(credentials)
} catch (e: CredentialsManagerException) {
e.printStacktrace()
}
If the token is expired, it gets a new one and saves it to local storage.Aleksei Tirman [JB]
05/29/2026, 6:20 AMloadTokens may not be called when necessary to trigger the Auth0 functionality.