felangel / fresh

🍋 A token refresh library for Dart.
https://github.com/felangel/fresh
349 stars 56 forks source link

Retry the http request upon refresh #106

Open feinstein opened 1 month ago

feinstein commented 1 month ago

I looked at the code, maybe I missed it and I apologise, but it would be better if the library had an option to retry the same http request if it detected a token refresh was necessary (e.g. Got a 401 error, did a refresh, then send the old request again with the new token).

Also include a timer to try to refresh before expiring, so there's less chance for an http request will fail.

uzuki-P commented 1 month ago

Upon checking out the code and also some testing using Alice, fresh actually did resend the request.

But on this line I think it's better to use copyWith to make it more readable and it will create new object, something like this

final requestOptions = response.requestOptions.copyWith();
requestOptions.headers = requestOptions.headers..addAll(_tokenHeader(refreshedToken));
_httpClient.fetch(requestOptions);

I believe request is also using fetch under the hood.