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

refreshToken not called at all? #58

Open
tazik561 opened this issue Jan 6, 2022 · 1 comment
Open

refreshToken not called at all? #58

tazik561 opened this issue Jan 6, 2022 · 1 comment

Comments

@tazik561
Copy link

tazik561 commented Jan 6, 2022

I am using this library like this:

  Future<GraphQLClient> _getClient({
    required final String path,
    final String? accessToken,
  }) async {
    // final Link link = HttpLink(path);
    // Link link = HttpLink(path, httpClient: LoggerHttpClient(http.Client()));
    // if (accessToken != null) {
    //   link = AuthLink(getToken: () async => 'Bearer $accessToken').concat(
    //     HttpLink(
    //       path,
    //     ),
    //   );
    // }
    final freshLink = FreshLink.oAuth2(
      shouldRefresh: (final _) {
        debugPrint(_.errors.toString());
        debugPrint(_.data.toString());
        return true;
      },
      tokenStorage: InMemoryTokenStorage(),
      refreshToken: (final token, final client) {
        // Perform refresh and return new token
        debugPrint('--------->refreshToken  $token');

        return Future.value();
      },
    )..authenticationStatus.listen(
        (final data) {
          debugPrint('---------> authenticationStatus $data');
        },
        onError: (final err) {
          debugPrint('--------->onError $err');
        },
      );
    await freshLink.setToken(OAuth2Token(accessToken: 'Bearer $accessToken'));
    return getClient = GraphQLClient(
      link: Link.from([freshLink, HttpLink(path)]),
      cache: GraphQLCache(store: InMemoryStore()),
    );
  }

  Future<QueryResult> query({
    required final String mainPath,
    required final String query,
    final Function(Map<String, dynamic>? result)? action,
    final Function(String message)? error,
    final bool requiredToken = true,
    final bool showDialogResend = false,
    final Map<String, dynamic>? params,
    final FetchPolicy fetchPolicy = FetchPolicy.networkOnly,
  }) async {
    // try {
    // Get token from local storage if required token is true
    UserTokenModel? token;
    if (requiredToken) {
      token = UserStoreService.to.getToken();
    }

    final client = await _getClient(
      path: mainPath,
      accessToken: requiredToken ? token!.accessToken : null,
    );
    return client.query(
      QueryOptions(
        document: gql(query),
        fetchPolicy: fetchPolicy,
        variables: params ?? {},
      ),
    );
  }

But just this print is printed:
---------> authenticationStatus AuthenticationStatus.unauthenticated
and other callback not triggered?
I am using unit test to test that?

@tazik561
Copy link
Author

tazik561 commented Jan 6, 2022

I fix the problem by change await freshLink.setToken(OAuth2Token(accessToken: 'Bearer $accessToken'));
to this await freshLink.setToken(OAuth2Token(accessToken: '$accessToken'));

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

1 participant