Refresh token within interceptor #58
-
Hi, would you be willing to share any ideas how to achieve refresh token functionality within interceptor? Or how to do it in general using Nylo? I am trying to do it in the following way:
I am able to successfully refresh the token and set it in the request options but I have no idea how I can retry the request that failed in the first place. Any help is much appreciated, thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @voytech-net, Under the hood, Nylo uses class ExampleInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
return super.onRequest(options, handler);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
handler.next(response);
}
@override
void onError(DioException dioException, ErrorInterceptorHandler handler) async {
if (dioException.response?.statusCode == 401) {
// If a 401 response is received, refresh the access token
String newAccessToken = await ApiService().refreshToken();
// Update the request header with the new access token
dioException.requestOptions.headers['Authorization'] = 'Bearer $newAccessToken';
// Repeat the request with the updated header
dynamic response = api<BaseApiService>((request) => request.network(request: (dio) {
dio.request(
dioException.requestOptions.path,
data: dioException.requestOptions.data,
queryParameters: dioException.requestOptions.queryParameters,
options: Options(
method: dioException.requestOptions.method,
headers: dioException.requestOptions.headers,
),
);
return dio;
}));
return handler.resolve(response);
}
return handler.next(dioException);
}
} |
Beta Was this translation helpful? Give feedback.
Hi @voytech-net,
Under the hood, Nylo uses
Dio
.You can try this.