Skip to content

Instantly share code, notes, and snippets.

@MrLoh
Created March 21, 2018 16:11
Show Gist options
  • 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;
@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.

@osmankaradeniz
Copy link

Hi everyone,

We solved this problem using PubSub. After the first login, the client subscribes to the refresh_token event. On the backend, we check the token's expiration time with every request. If the token is set to expire within 1 hour, we publish a new token to the client. During the login process, we set a unique event name in Redis for each client, and the client subscribes to this unique event name. When the conditions for refreshing the token are met, we publish the new token to the specified event name, and the client catches the publication and updates the local storage.

This is a solution we came up with on our own, and we are open to any ideas and feedback you may have. Thank you.

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