Hi everyone, I'm facing an issue where the token i...
# multiplatform
a
Hi everyone, I'm facing an issue where the token in my Ktor client doesn't update automatically after a successful login. I've verified that the token is properly updated in my SessionHandler, so the issue doesn't seem to be with the session handling. The problem is that my Ktor client only initializes the token once during app startup, and it doesn’t get triggered again after login. As a result, the token remains null in the client even though it has been updated in the SessionHandler. Here’s the relevant code for my MbakKasirHttpClientBuilder:
Copy code
class MbakKasirHttpClientBuilder(
    private val sessionHandler: SessionHandler
) {

    private lateinit var protocol: URLProtocol
    private lateinit var host: String
    private var port: Int? = null

    fun protocol(protocol: URLProtocol) = apply { this.protocol = protocol }
    fun host(host: String) = apply { this.host = host }
    fun port(port: Int) = apply { this.port = port }

    fun build(engine: HttpClientEngine): HttpClient {
        return HttpClient(engine) {

            expectSuccess = true

            defaultRequest {
                url {
                    protocol = this@MbakKasirHttpClientBuilder.protocol
                    host = this@MbakKasirHttpClientBuilder.host
                    this@MbakKasirHttpClientBuilder.port?.let { port = it }
                }
                header("Content-Type", "application/json")
            }

            install(ContentNegotiation) {
                json(
                    Json {
                        prettyPrint = true
                        isLenient = true
                        ignoreUnknownKeys = true
                    }
                )
            }

            install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        println(message)
                    }
                }
                level = LogLevel.ALL
            }

            install(Auth) {
                bearer {
                    loadTokens {
                        BearerTokens(
                            accessToken = sessionHandler.getToken().first(),
                            refreshToken = ""
                        )
                    }
                }
            }
        }
    }
}
I inject the HttpClient using Koin like this:
Copy code
val provideHttpClientModule = module {
    single {
        MbakKasirHttpClientBuilder(sessionHandler = get())
            .protocol(URLProtocol.HTTPS)
            .host(BuildKonfig.BASE_URL)
            .build(engine = get())
    }

    single { RequestHandler(get()) }
}
Does anyone know how to make the Ktor client dynamically observe and use the updated token after login? Should I recreate the client after login, or is there a better way to make the Auth plugin fetch the latest token automatically? Any advice or guidance would be greatly appreciated!
p
I would suggest asking in #C0A974TJ9, maybe @Ignat Beresnev can help.
a
Oh okay thanks
w
Try creating an Auth Interceptor