felangel / fresh

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

[fresh_grahpql] Multiple request #85

Closed niti-alzhemist closed 1 year ago

niti-alzhemist commented 1 year ago

If I have 4 network requests and response is UNAUTHENTICATED, shouldRefresh will return true, all of requests will call refreshToken() ()to get the new OAuth2Token then retry the request.

Is it possible to call refreshToken only first time then retry all request?

Star-Lord-76 commented 1 year ago

If I have 4 network requests and response is UNAUTHENTICATED, shouldRefresh will return true, all of requests will call refreshToken() ()to get the new OAuth2Token then retry the request.

Is it possible to call refreshToken only first time then retry all request?

Did you find the solution ?

Stephen-Strange commented 1 year ago

Any update @felangel ??

niti-alzhemist commented 1 year ago

I solved by using mutex (https://pub.dev/packages/mutex), locking the request when the first refresh job is called then release and inject new token when receive it @Star-Lord-76 @Stephen-Strange

Star-Lord-76 commented 1 year ago

I solved by using mutex (https://pub.dev/packages/mutex), locking the request when the first refresh job is called then release and inject new token when receive it @Star-Lord-76 @Stephen-Strange

Please attach example @niti-alzhemist

fodedoumbouya commented 5 months ago

I solved by using mutex (https://pub.dev/packages/mutex), locking the request when the first refresh job is called then release and inject new token when receive it @Star-Lord-76 @Stephen-Strange

thank you

example

final m = Mutex();

AuthLink authLink = AuthLink(
      getToken: () async {
        print("lock");
        await m.acquire();
        final token = await getAccessToken();
        m.release();
        print("unlock");
        debugPrint("Bearer ${token == null ? "is" : "is not"} Null");
        if (token == null) return null;
        return 'Bearer $token';
      },
    );