Skip to content

Commit

Permalink
Do not try to automatically invalidate the current token and fully re…
Browse files Browse the repository at this point in the history
…ly on the Session Expired dialog
  • Loading branch information
paales committed Feb 1, 2024
1 parent ee969bf commit 09a2cc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-hornets-study.md
Original file line number Diff line number Diff line change
@@ -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
46 changes: 12 additions & 34 deletions packages/magento-customer/typePolicies.ts
Original file line number Diff line number Diff line change
@@ -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<CustomerToken['valid']> = {
read(existing, options) {
if (existing === undefined) return existing

const ref = options.toReference({ __ref: 'CustomerToken' })
const createdAt = options.readField<string>('createdAt', ref)

if (!createdAt) return existing

return new Date().getTime() - new Date(createdAt).getTime() < TOKEN_EXPIRATION_MS
},
}

const generateCustomerToken: FieldPolicy<Mutation['generateCustomerToken']> = {
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) => {
Expand Down

0 comments on commit 09a2cc3

Please sign in to comment.