authenticate method
override
Returns a Request that includes credentials to satisfy
an authentication challenge received in response, based on
the incoming request or optionally, the originalRequest
(which was not modified with any previous Interceptors).
Otherwise, return null if the challenge cannot be satisfied.
Implementation
@override
FutureOr<Request?> authenticate(
Request request,
Response response, [
Request? originalRequest,
]) async {
if (response.statusCode < 400 || response.statusCode >= 500) {
return null;
}
final handlers = _handlers.toList();
for (final handler in handlers) {
Request? updatedRequest;
try {
updatedRequest = await handler(request, response, originalRequest);
} catch (error, stackTrace) {
if (kDebugMode) {
print('An error occurred while authenticating. Trying next handler.'
'\n\n$error\n\n$stackTrace');
}
continue;
}
if (updatedRequest != null) {
return updatedRequest;
}
}
return null;
}