diff --git a/.changeset/twenty-hornets-study.md b/.changeset/twenty-hornets-study.md new file mode 100644 index 0000000000..baa9195d01 --- /dev/null +++ b/.changeset/twenty-hornets-study.md @@ -0,0 +1,5 @@ +--- +"@graphcommerce/magento-customer": patch +--- + +Do not try to automatically invalidate the current token and fully rely on the Session Expired dialog diff --git a/packages/magento-customer/typePolicies.ts b/packages/magento-customer/typePolicies.ts index 3c0fff0ff1..6c53cc9b6f 100644 --- a/packages/magento-customer/typePolicies.ts +++ b/packages/magento-customer/typePolicies.ts @@ -1,53 +1,31 @@ import type { FieldPolicy, MigrateCache, StrictTypedTypePolicies } from '@graphcommerce/graphql' -import type { CustomerToken, Mutation } from '@graphcommerce/graphql-mesh' +import type { Mutation } from '@graphcommerce/graphql-mesh' import { CustomerTokenDocument } from './hooks/CustomerToken.gql' -import { IsEmailAvailableDocument } from './hooks/IsEmailAvailable.gql' - -const TOKEN_EXPIRATION_MS = 60 * 60 * 1000 - -const valid: FieldPolicy = { - read(existing, options) { - if (existing === undefined) return existing - - const ref = options.toReference({ __ref: 'CustomerToken' }) - const createdAt = options.readField('createdAt', ref) - - if (!createdAt) return existing - - return new Date().getTime() - new Date(createdAt).getTime() < TOKEN_EXPIRATION_MS - }, -} const generateCustomerToken: FieldPolicy = { keyArgs: () => '', merge(_existing, incoming, options) { if (!options.isReference(incoming)) return incoming - const write = () => { - options.cache.writeQuery({ - query: CustomerTokenDocument, - broadcast: true, - data: { - customerToken: { - __typename: 'CustomerToken', - token: options.readField('token', incoming) as string, - createdAt: new Date().toUTCString(), - valid: true, - }, + options.cache.writeQuery({ + query: CustomerTokenDocument, + broadcast: true, + data: { + customerToken: { + __typename: 'CustomerToken', + token: options.readField('token', incoming) as string, + createdAt: new Date().toUTCString(), + valid: true, }, - }) - } - write() + }, + }) - // Broadcasts the query after the token expiration so UI gets updated - setTimeout(write, TOKEN_EXPIRATION_MS) return incoming }, } export const customerTypePolicies: StrictTypedTypePolicies = { Mutation: { fields: { generateCustomerToken } }, - CustomerToken: { fields: { valid } }, } export const migrateCustomer: MigrateCache = (oldCache, newCache) => {