Hello, I have encountered a problem with asynchron...
# ktor
p
Hello, I have encountered a problem with asynchronous call requests. Several refreshTokens are executed during the 401 code. Occasionally a situation occurs where an old token is used and I get code 400. Does anyone have a solution to solve this? I was under the impression that this queue is solved by Ktor.
Copy code
refreshTokens {
                    client.authProvider<BearerAuthProvider>()?.clearToken()
                    val oldTokens = applicationPersistence.getTokens()
                    val response = kotlin.runCatching {
                        client.submitForm(
                            FlavorConstants.authUrl + "token",
                            formParameters = parameters {
                                append("grant_type", "refresh_token")
                                append("client_id", FlavorConstants.authClientId)
                                append("refresh_token", oldTokens?.refreshToken.orEmpty())
                            },
                        ) { markAsRefreshTokenRequest() }
                    }.getOrElse {
                        oldTokens?.also {
                            applicationPersistence.saveTokens(
                                accessToken = it.accessToken,
                                refreshToken = it.refreshToken,
                                idToken = it.idToken,
                            )
                        }
                        throw it
                    }

                    if (response.status == HttpStatusCode.OK) {
                        val body = response.body<TokenResponse>()
                        applicationPersistence.saveTokens(
                            accessToken = body.accessToken,
                            refreshToken = body.refreshToken,
                            idToken = body.idToken,
                        )
                        applicationPersistence.getTokens()?.let {
                            BearerTokens(it.accessToken, it.refreshToken)
                        } ?: run {
                            onNotAuthorized()
                        }
                    } else {
                        onNotAuthorized()
                    }
                }
a
Can you please file an issue with a more detailed description of how to reproduce the problem,?
👍 1
p
If you need more information please let me know. I do not know how to explain better https://youtrack.jetbrains.com/issue/KTOR-7852/The-refresh-token-is-triggered-several-times-during-async-calls