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

Paul Weber

12/22/2021, 12:50 PM
I use ktor client with the Bearer Auth plugin. I have one
HttpClient
that I instantiate at startup and hand over to all my remote data source classes via Koin. So basically a singleton. 1. Is that the best practice or should I create a client for every request? Or something else? With that approach I’m running into the problem of not being able to “logout” so to speak. As in: the
bearerAuth.loadTokens
gets called once on creation instead of on every web request. 2. How can I remove the bearer token from the
HttpClient
, so that it knows to
loadTokens
once again? 3. If my approach with just one
HttpClient
at the same time is valid and it’s not possible to remove the tokens, would it be proper to just create a new
HttpClient
on every logout? It would certainly be the easy way out, but is there something better?
r

Rustam Siniukov

12/22/2021, 3:24 PM
It’s possible to clear the token, with this ugly piece of code
Copy code
client.plugin(Auth)!!.providers.filterIsInstance<BearerAuthProvider>().first().clearToken()
👍 2
p

Paul Weber

12/22/2021, 3:40 PM
That sounds promising, on what interface / class is
.plugin
declared? I can’t find it on my
HttpClient
I’m using version
1.6.7
r

Rustam Siniukov

12/22/2021, 3:56 PM
in 1.6.7 you can use
client[Auth]
syntax
p

Paul Weber

12/22/2021, 3:58 PM
lovely, thank you so much!
👍 1
92 Views