felangel / fresh

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

Retry the http request upon refresh #106

Open feinstein opened 4 months ago

feinstein commented 4 months 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 3 months 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.