Skip to content

Instantly share code, notes, and snippets.

@MrLoh
Created March 21, 2018 16:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MrLoh/1ae9e48ceb595207ecb3cfdb9849c083 to your computer and use it in GitHub Desktop.
Save MrLoh/1ae9e48ceb595207ecb3cfdb9849c083 to your computer and use it in GitHub Desktop.
// @flow
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { RetryLink } from 'apollo-link-retry';
import { AuthLink } from './link-auth';
import cache from './cache';
const retryLink = new RetryLink();
const authLink = new AuthLink();
const httpLink = new HttpLink({ uri: CINURU_API });
const link = ApolloLink.from([reduxLoggerLink, retryLink, authLink, httpLink]);
// construct client
const client = new ApolloClient({ link, cache });
// inject client dependencies
authLink.injectClient(client);
export default client;
@ltouro
Copy link

ltouro commented Mar 29, 2018

How one can access the Response Headers when netowrkError.statusCode === 401? For instance, the response contains the WWW-Authenticate header that indicates how to proceed with token refresh.

@alexmbp
Copy link

alexmbp commented Apr 5, 2018

I have a little different implementation of that, and I just curios, why we need this construction? Can we just create Promise every time? I was thinking, that's probably needed to prevent redundant token refreshes?

refreshToken = (): Promise<boolean> => {
    if (!this.tokenRefreshingPromise) this.tokenRefreshingPromise = refreshToken(this.client);
    return this.tokenRefreshingPromise;
};

@jason-shen
Copy link

do you mind sharing the import { getToken, refreshToken } from '../services/auth' file? cheers

@techyrajeev
Copy link

@MrLoh error handler for subscription is not getting called

@verzil
Copy link

verzil commented Aug 30, 2018

you never set the this.tokenRefreshingPromise to null after calling refreshToken. So does this mean it will only refreshToken once during application lifetime?

@gordonk
Copy link

gordonk commented Oct 5, 2018

you never set the this.tokenRefreshingPromise to null after calling refreshToken. So does this mean it will only refreshToken once during application lifetime?

Yes it does. Solution is to reset the tokenRefreshingPromise in the observer return function, along with the two subscriptions.

if (this.tokenRefreshingPromise) this.tokenRefreshingPromise = null

FWIW I got this working nicely and appreciate the pattern. Bit nervous about updating apollo-link now though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment