Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fresh_graphql] shouldRefresh example needed #81

Open
subzero911 opened this issue Mar 31, 2023 · 4 comments
Open

[fresh_graphql] shouldRefresh example needed #81

subzero911 opened this issue Mar 31, 2023 · 4 comments

Comments

@subzero911
Copy link

Can't figure out how to implement a shouldRefresh condition.
shouldRefresh: (response) => response.errors... // somehow check if a token is stale ?

@Y-ndm
Copy link

Y-ndm commented Apr 23, 2023

shouldRefresh: (_) {
        print("SHOULD REFRESH TOKEN?");
        if (token == null || token == '') {
          print("SHOULD REFRESH TOKEN? 2");
          return true;
        } else {
          if (JwtDecoder.isExpired(token ?? '')) {
            print("TOKEN IS EXPIRED");
            return true;
          } else {
            print("TOKEN IS NOT EXPIRED");
            return false;
          }
        }
      },

@Y-ndm
Copy link

Y-ndm commented Apr 23, 2023

shouldRefresh: (_) {
        print("SHOULD REFRESH TOKEN?");
        if (token == null || token == '') {
          print("SHOULD REFRESH TOKEN? 2");
          return true;
        } else {
          if (JwtDecoder.isExpired(token ?? '')) {
            print("TOKEN IS EXPIRED");
            return true;
          } else {
            print("TOKEN IS NOT EXPIRED");
            return false;
          }
        }
      },

Here is JwtDecoder.

@steveroseik
Copy link

Can you provide it in pub.dev example for other people? the example is useful and would definitely benefit others

@subzero911
Copy link
Author

That's how it's implemented in my project.
The backend sends the custom error code E_TOKEN_INACTIVE, when it's expired

shouldRefresh: (response) {
      final errors = response.errors;
      return errors != null &&
          errors.isNotEmpty &&
          errors.firstWhereOrNull((e) => e.extensions?['code'] == GraphqlErrorCodes.eTokenInactive) != null;
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants