https://kotlinlang.org logo
Title
h

Hossein Amini

05/03/2023, 10:41 AM
How is it possible to recall
loadTokens
again after the user logs in successfully in Bearer Authentication? Normally when the user opens the app the loadTokens will be triggered and because there is no saved access token it returns
BearerTokens(null, null)
. After the user logs in I save the access and refresh token in the database but loadTokens won’t be triggered again to set new access and refresh token. How can I force the Ktor to trigger
loadTokens
again after the user logs in?
install(Auth) {
    bearer {
        loadTokens {
            // Some code to retrieve access and refresh token from the database
            BearerTokens(accessToken, refreshToken)
        }
    }
}
r

Rustam Siniukov

05/03/2023, 10:44 AM
loadTokens
block should return
null
where there is no valid token
h

Hossein Amini

05/03/2023, 10:56 AM
Ok, I changed my code to return null in case there in no saved access token but it still does not trigger loadTokens on every request to check if there is any new access and refresh tokens.
r

Rustam Siniukov

05/03/2023, 11:04 AM
then you can use this code after you save your tokens
client.plugin(Auth).providers.filterIsInstance<BearerAuthProvider>().first().clearToken()
it’s ugly, but should work
h

Hossein Amini

05/03/2023, 11:11 AM
It works even though it’s not that nice 😄 Thank you bro
Should I use this code after updating access token using refresh token?
r

Rustam Siniukov

05/03/2023, 11:19 AM
no, it should update automatically