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

[QUESTION] - How to do a token refresh? #672

Closed
brenoasm opened this issue Jun 17, 2020 · 6 comments
Closed

[QUESTION] - How to do a token refresh? #672

brenoasm opened this issue Jun 17, 2020 · 6 comments

Comments

@brenoasm
Copy link

Hi, I've been using It to build an personal project and finding hard to implement a refresh process. Using Dio I can block the client so all the subsequence requests goes to a queue and then after unlock the queue is processed. Using graphql_flutter do I need to implement something like that by myself? If someone has some example to show me. Thanks in advance.

@Gujie-Novade
Copy link

The way I did was:
When you configure the AuthLink like in the doc, do something like:

final AuthLink authLink = AuthLink(
    getToken: () async  {
       final token = await getAccessToken();
       return `Bearer $token`
    },
  );

And the getAccesstoken() function does the following:

  1. retrieve the access token from wherever you stored it
  2. parse it and check the expiry
    2.a if it's expired, renew it by making a HTTP call to your server with your refresh token
  3. return the access token

@micimize
Copy link
Collaborator

@brenoasm we don't have an offline operation queue implemented in core (for AuthLink or otherwise), and I haven't seen any elsewhere either.

@brenoasm
Copy link
Author

@Gujie-Novade Cool, what happen's If multiple requests are sent with the invalid token? All the requests pass by the getToken?

@micimize Sorry for the question, but I'not expert using Apollo. Would not be the case to implement such routine? Dio for example has It built in (I mean the queue and way to lock and unlock the client), os it not usefull while using graphql? I did the queue process in my application after I've posted this question, It works but i'm not sure if It is the right way to do.

@micimize
Copy link
Collaborator

@brenoasm The client doesn't have any awareness of the relatedness of the failures, so unless you set a flag in your code based on an auth error it will continue along as normal.

It'd definitely be useful, but is just non-trivial. A more complicated offline queue is desirable for even more reasons in graphql (#201)

You could make a custom AuthLink with refresh state/logic that waits to call forward until the new token is gotten. If you go that route I'm sure it'll be quite use useful to others. It looks like there's some discussion/potential solutions for retry logic in #220 as well

Idk how useful this is but on the v4 alpha we use gql_links, for which there is a dio link.

@brenoasm
Copy link
Author

Thanks for the response! I did the retry logic inside a custom link, like you said. I was not aware about the v4 and If It let me use an Dio as client, for sure all about I said is built in. I'll close this issue.

@Aristidios
Copy link

And the getAccesstoken() function does the following:

  1. retrieve the access token from wherever you stored it
  2. parse it and check the expiry
    2.a if it's expired, renew it by making a HTTP call to your server with your refresh token
  3. return the access token

Hello,

Would you mind sharing your implementation of getAccesstoken() 🙂

@Gujie-Novade

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

4 participants