fix token refresh
import ApolloClient from 'apollo-client';
-import {ApolloLink} from 'apollo-link';
+import {ApolloLink, Observable} from 'apollo-link';
import {InMemoryCache} from 'apollo-cache-inmemory';
import {createHttpLink} from 'apollo-link-http';
import {onError} from 'apollo-link-error';
@@ -26,9 +26,12 @@ export const SERVER =
const httpLink = createHttpLink({uri: SERVER});
const authMiddleware = new ApolloLink((operation, forward) => {
+ return new Observable(observable => {
+ let sub = null;
+ this.c._auth.jwt().then(t => {
//The signup flow accepts a token
let qs = queryString.parse(document.location.search);
- let token = qs.token || localStorage.getItem('id_token');
+ let token = qs.token || t;
if (token) {
operation.setContext(({headers = {}}) => ({
@@ -39,7 +42,11 @@ const authMiddleware = new ApolloLink((operation, forward) => {
}));
}
- return forward(operation);
+ sub = forward(operation).subscribe(observable);
+ });
+ //TODO: I think this is always null...
+ return () => (sub ? sub.unsubscribe() : null);
+ });
});
const stackdriverMiddleware = new ApolloLink((operation, forward) => {
