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

Add a way to set a custom middleware for refreshing the access token #86

Closed
lassesteffen opened this issue Feb 22, 2019 · 7 comments
Closed
Labels
enhancement New feature or request

Comments

@lassesteffen
Copy link

Currently we are not able to add a custom middleware for e. g. refreshing an access token. This stems from the fact, that the authLink is the first link, and I can not easily add another link before this authLink (which sets the token).

Am I missing something or is there just no support for this?

@Akryum
Copy link
Owner

Akryum commented Oct 20, 2019

Do you have an example of how it would look like?

@lassesteffen
Copy link
Author

lassesteffen commented Oct 21, 2019

Thanks for the response! This is what I need and what I was not able to build with this plugin. I think the example is pretty : straight forward:

const refreshTokenLink = setContext(() => AuthenticationService.renewSessionIfExpired());

const authLink = setContext((_, { headers }) => {
  const { accessToken } = store.state.user;
  const newHeaders = {
    ...headers,
  };
  if (accessToken !== null) {
    newHeaders['authorization'] = `JWT ${accessToken}`;
  }

  return {
    headers: newHeaders,
  };
});

const httpLink = createUploadLink({ uri: httpEndpoint });

const link = ApolloLink.from([refreshTokenLink, authLink, httpLink]);

In the code the first link is always the AuthLink. If there would be some option like a preAuthLink, that could be passed in that would be awesome.

{
    ....

    const authLinkChain = authLink
    if (preAuthLink) {
       authLinkChain = preAuthLink.concat(authLink)
    }
    
    // Concat all the http link parts
    link = authLinkChain.concat(link)

    ....
  }

@matrunchyk
Copy link
Contributor

matrunchyk commented Oct 21, 2019

@lassesteffen please take a look on this PR and check whether it would work for your case.

It allows to pass a few pre-auth links. So I think using like the following should make the deal:

// ...
preAuthLinks: [refreshTokenLink],
// ...

@lassesteffen
Copy link
Author

Looks good to me!

@matrunchyk
Copy link
Contributor

matrunchyk commented Oct 21, 2019

Great!
@Akryum are you okay with merging the relevant PR then?

@matrunchyk
Copy link
Contributor

Maybe other upvoters can also check this @Durss, @oller, @madmod, @thetre97

@Akryum Akryum closed this as completed in d6aa49e Jun 8, 2020
@boydaihungst
Copy link

boydaihungst commented Aug 21, 2020

@matrunchyk You forgot to add .concat(link)
It should be

if (preAuthLinks.length) { 
      link = from(preAuthLinks).concat(link)
}

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

No branches or pull requests

4 participants