3

I am using Ktor client for Android together with the plugin: io.ktor:ktor-client-auth:1.6.4. The current implementation is similar to this snippet.

Now I want to implement a 'log out' function when after a button is clicked the tokens are deleted, the question is... how?

JaySH
  • 546
  • 6
  • 15

2 Answers2

12

In case you are looking for the same functionality in ktor version 2.0+

val client = HttpClient(Apache) {
    install(Auth) {
        bearer {}
    }
}
    
client.plugin(Auth).providers.filterIsInstance<BearerAuthProvider>()
                .firstOrNull()?.clearToken()
Ruslan Run
  • 176
  • 2
  • 6
4

You can get an instance of the Auth plugin, find a BearerAuthProvider provider and call the clearToken method to delete tokens. Here is an example:

val client = HttpClient(Apache) {
    install(Auth) {
        bearer {}
    }
}

val provider = client.feature(Auth)!!.providers.filterIsInstance<BearerAuthProvider>().first()
provider.clearToken()
Aleksei Tirman
  • 4,658
  • 1
  • 5
  • 24