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

Pwa 3154 v1 - Donot merge #4161

Closed
wants to merge 21 commits into from
53 changes: 42 additions & 11 deletions packages/peregrine/lib/Apollo/links/authLink.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import { setContext } from '@apollo/client/link/context';
import { ApolloLink } from '@apollo/client';
import { BrowserPersistence } from '@magento/peregrine/lib/util';

const storage = new BrowserPersistence();
var token = storage.getItem('signin_token') || null;
var gg = 'dd';
export class MagentoGQLCacheLink extends ApolloLink {
// The token get reinstantiated on refresh.
// If we have an existing token value from a previous browsing session, use it.

export default function createAuthLink() {
return setContext((_, { headers }) => {
// get the authentication token from local storage if it exists.
const token = storage.getItem('signin_token');

request(operation, forward) {
operation.setContext(previousContext => {
const { headers } = previousContext;
// return the headers to the context so httpLink can read them
console.log(headers+'sdfsd');
console.log('kjhg');
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
TTd: gg
}
};
});

// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ''
// Update the token from each response.
const updateToken = data => {
const context = operation.getContext();
const { response } = context;
// console.log(string.indexOf(substring) !== -1); // true

if (response.headers.get('X-Cache') == 'HIT, HIT') { console.log('cache');
this.token = null;
this.gg = 'dff';
}
this.gg = response.headers.get('X-Cache');
console.log(token);
// Purposefully don't modify the result,
// no other link needs to know about the cache id.
return data;
};
});

return forward(operation).map(updateToken);
}
}

export default function createAuthLink() {
return new MagentoGQLCacheLink();
}