Failure to obtain token results in an infinite loop
What did you do?
I have the following tokenProvider in my app:
private lazy var tokenProvider: TokenProvider = { [weak self] completion in
_ = self?.authRepository.chatToken()
.observeOn(MainScheduler.instance)
.subscribe(
onSuccess: { response in
if let chatToken = response.chatToken {
completion(.success(Token(stringLiteral: chatToken)))
do {
let decoder = JWTDecoder()
let jwtData = try decoder.decode(jwtToken: chatToken)
if let exp = jwtData["exp"] as? TimeInterval {
self?.tokenExpiryTime = exp
self?.tokenExpiryDate = Date(timeIntervalSince1970: exp)
}
} catch {
completion(.failure(AppErrors.generic))
}
} else {
completion(.failure(AppErrors.generic))
}
},
onError: { error in
completion(.failure(error))
}
)
}
If the authRepository.chatToken() fails with, for example, a 401 error, the stream chat will start repeatedly calling the tokenProvider and failing.
What did you expect to happen?
The completion handler of the connectUser to be called with an error. Or a way to break out of the loop.
What happened instead?
An infinite loop.
GetStream Environment
GetStream Chat version: 4.52.0 GetStream Chat frameworks: StreamChat, StreamChatUI iOS version: 17.5.1 Swift version: 5.8.1 Xcode version: 15.3 Device: iPhone 12
Hi @pavermakov,
This issue is already fixed on more recent versions. Please update the SDK to the latest version.
Thank you, Nuno
Hi @nuno-vieira! Bumped version up to 4.63.0 and observed the same behaviour. The tokenProvider is called over and over again.
Hi @pavermakov,
It is true, I can reproduce this. I will re-open it. We will let you know once we work on this issue. At the moment I can't provide you with an ETA.
Best, Nuno
Thank you @nuno-vieira, I appreciate that! Also, I have noticed that the class AuthenticationRepository has constant maximumTokenRefreshAttempts set to 10. Is it possible to customise this value?
Hi @pavermakov, no, but you can provide a timeout through ChatClientConfig.reconnectionTimeout
Hi @pavermakov, are you still able to reproduce this? I'm not at the moment, the token provider is only called 10 times as per the maximumTokenRefreshAttempts. Also, make sure that if the response has a 401 status, that you return an error, and not a success on the Token Provider response.
@nuno-vieira I no longer get an infinite loop, just invoking the token provider 10 times.
Awesome 🙏
I'll close this one then 👍