Pallaw Pathak
11/15/2023, 5:43 PMrefreshTokens
works while using Auth
plugin with bearer
provider? In my understanding it should be invoked as soon as server returns 401 in the response. Unfortunately its not working for me. Can anyone please help
code for reference
fun RemoteDataModule() =
module {
single<HttpClient> {
HttpClient {
// converts response into dto objects
install(ContentNegotiation) {
json(HttpClientUtil.nonStrictJson)
}
// Log HTTP request and response
install(Logging) {
logger = HttpClientLogger // custom logger
level = LogLevel.ALL
}.also { initLogger() } // this will initialise napier for both platform
//Add authentication header in the request
install(Auth) {
bearer {
loadTokens {// add BearerTokens in header
val appPreferenceDataSource = get<AppPreferencesDataSource>()
val accessToken = appPreferenceDataSource.getAccessToken().first() ?: ""
val refreshToken = appPreferenceDataSource.getRefreshToken().first() ?: ""
BearerTokens(accessToken, refreshToken)
}
/**
* This block is called when API returns 401, UnAuthorized
* In that case it will automatically hit the same API with updated token
*/
refreshTokens {
// Refresh tokens and return them as the 'BearerTokens' instance
val appPreferenceDataSource = get<AppPreferencesDataSource>()
val accessToken = appPreferenceDataSource.getAccessToken().first() ?: ""
val refreshToken = appPreferenceDataSource.getRefreshToken().first() ?: ""
BearerTokens(accessToken, refreshToken)
}
}
}
defaultRequest {
// Set the Content-Type header for each request
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header("Client-id", AppConstants.UNIGO_ORG_ID)
}
}
}
single<KtorApiService> { KtorApiServiceImp(get<HttpClient>()) }
}
Alexander Zhirkevich
11/15/2023, 6:27 PMPallaw Pathak
11/15/2023, 6:52 PMAleksei Tirman [JB]
11/16/2023, 7:39 AMWWW-Authenticate
header is present and contains the Bearer
auth scheme?Pallaw Pathak
11/16/2023, 7:44 AMWWW-Authenticate
is only needed when multiple providers are used, Otherwise just 401 should be enough to call the refreshToken{}
block.
Please correct me if I am wrong here.
Attached is the SS of the official document for reference.Aleksei Tirman [JB]
11/16/2023, 7:48 AMPallaw Pathak
11/16/2023, 7:51 AMkoin
for DI and creating the client as single
instance. Can it be a problem here?Aleksei Tirman [JB]
11/16/2023, 7:52 AM