https://kotlinlang.org logo
#ktor
Title
# ktor
a

Adam Brown

03/10/2023, 10:07 PM
I'm using Bearer Token auth with the ktor client, but cant quite figure this out: when i login in and get my new token i save it to storage. but the
HttpClient
it's self doesn't know it needs to call it's
loadTokens
again, is there anyway to get it to reload it's tokens after a login?
🦻 1
o

Olivier Patry

03/11/2023, 9:21 AM
I went for something like that
Copy code
fun HttpClient.updateCredentials(credentials: Credentials) {
    val authPlugin = plugin(Auth)
    authPlugin.providers.removeAll { true }
    when (credentials) {
        is Credentials.Token -> authPlugin.bearer {
            loadTokens {
                credentials.bearerTokens
            }
        }

        is Credentials.Basic -> authPlugin.basic {
            credentials.basicAuthCredentials
        }
    }
}
I have an extension to convert my
Credentials
to proper ktor version (
BasicAuthCredentials
and
BearerTokens
)
a

Adam Brown

03/11/2023, 8:32 PM
oh perfect, just what i was looking for!
95 Views