androidkotlinktorktor-client

How to clear bearer tokens in Ktor-client for Android


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?


Solution

  • 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()