@graphcommerce/magento-customer 11.0.0-canary.59 → 11.0.0-canary.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ## 11.0.0-canary.62
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2723](https://github.com/graphcommerce-org/graphcommerce/pull/2723) [`a96bbd7`](https://github.com/graphcommerce-org/graphcommerce/commit/a96bbd798e669dea841c63fd1733ae7bc6249a79) - A customer token with `valid: false` counts as signed out in `useCustomerSession`, `getPrivateQueryContext` and `usePrivateQueryContext`. `customerLink` does not send that token, so the shop showed guest prices in the layout for a customer and kept customer queries that failed. ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
8
+
9
+ - [#2724](https://github.com/graphcommerce-org/graphcommerce/pull/2724) [`8985281`](https://github.com/graphcommerce-org/graphcommerce/commit/89852812292471e6780188c83ee093cea4ed988c) - The session-expired form shows the email of the sign-in after a cache migration. The `CustomerToken` holds the `email` of the sign-in, and `migrateCustomer` copies it with the token. A shop that writes `CustomerTokenDocument` with an object literal must add `email`. ([@Giovanni-Schroevers](https://github.com/Giovanni-Schroevers))
10
+
11
+ ## 11.0.0-canary.61
12
+
13
+ ## 11.0.0-canary.60
14
+
3
15
  ## 11.0.0-canary.59
4
16
 
5
17
  ## 11.0.0-canary.58
@@ -5,5 +5,6 @@ query CustomerToken {
5
5
  createdAt
6
6
  valid
7
7
  xMagentoCacheId
8
+ email
8
9
  }
9
10
  }
@@ -1,7 +1,7 @@
1
1
  // @vitest-environment happy-dom
2
2
  import type * as ReactHookForm from '@graphcommerce/react-hook-form'
3
3
  import { act, renderHook } from '@testing-library/react'
4
- import { describe, expect, it, vi } from 'vitest'
4
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
5
5
  import { useAccountSignInUpForm } from './useAccountSignInUpForm'
6
6
 
7
7
  vi.mock('@graphcommerce/next-config/config', () => ({ enableGuestCheckoutLogin: true }))
@@ -9,9 +9,14 @@ vi.mock('@graphcommerce/framer-next-pages', () => ({ usePageContext: () => undef
9
9
  vi.mock('@graphcommerce/next-ui', () => ({ useUrlQuery: () => [{}, async () => {}] }))
10
10
  vi.mock('@graphcommerce/graphql', () => ({ useQuery: () => ({ data: undefined }) }))
11
11
  vi.mock('./useCustomerPermissions', () => ({ useCustomerAccountCanSignUp: () => true }))
12
- vi.mock('./useCustomerSession', () => ({
13
- useCustomerSession: () => ({ token: undefined, valid: false }),
12
+ const session = vi.hoisted(() => ({
13
+ current: {
14
+ token: undefined as string | undefined,
15
+ valid: false,
16
+ email: undefined as string | undefined,
17
+ },
14
18
  }))
19
+ vi.mock('./useCustomerSession', () => ({ useCustomerSession: () => session.current }))
15
20
  vi.mock('@graphcommerce/react-hook-form', async () => {
16
21
  const rhf = await vi.importActual<typeof ReactHookForm>('react-hook-form')
17
22
  return {
@@ -24,6 +29,18 @@ vi.mock('@graphcommerce/react-hook-form', async () => {
24
29
  })
25
30
 
26
31
  describe('useAccountSignInUpForm', () => {
32
+ beforeEach(() => {
33
+ session.current = { token: undefined, valid: false, email: undefined }
34
+ })
35
+
36
+ it('fills in the email of the expired session without a cached customer', () => {
37
+ session.current = { token: 'test-token', valid: false, email: 'customer@example.test' }
38
+ const { result } = renderHook(() => useAccountSignInUpForm())
39
+
40
+ expect(result.current.mode).toBe('session-expired')
41
+ expect(result.current.form.getValues('email')).toBe('customer@example.test')
42
+ })
43
+
27
44
  it('shows the password step after an email submitted with Enter', async () => {
28
45
  const { result } = renderHook(() => useAccountSignInUpForm())
29
46
 
@@ -18,7 +18,7 @@ export type AccountSignInUpState = 'email' | 'signin' | 'signup' | 'signedin' |
18
18
 
19
19
  export function useAccountSignInUpForm(props: UseFormIsEmailAvailableProps = {}) {
20
20
  const { onSubmitted } = props
21
- const { token, valid } = useCustomerSession()
21
+ const { token, valid, email: tokenEmail } = useCustomerSession()
22
22
 
23
23
  const canSignUp = useCustomerAccountCanSignUp()
24
24
  const isToggleMethod = !enableGuestCheckoutLogin || !canSignUp
@@ -26,7 +26,8 @@ export function useAccountSignInUpForm(props: UseFormIsEmailAvailableProps = {})
26
26
  const [queryState, setRouterQuery] = useUrlQuery<{ email?: string | null }>()
27
27
 
28
28
  const customerQuery = useQuery(UseCustomerValidateTokenDocument, { fetchPolicy: 'cache-only' })
29
- const cachedEmail = customerQuery?.data?.customer?.email
29
+ // The cache migration keeps the token and drops the customer.
30
+ const cachedEmail = customerQuery?.data?.customer?.email ?? tokenEmail
30
31
 
31
32
  const form = useFormGqlQuery<
32
33
  IsEmailAvailableQuery,
@@ -0,0 +1,50 @@
1
+ // @vitest-environment happy-dom
2
+ import { ApolloClient, ApolloLink, ApolloProvider, InMemoryCache } from '@graphcommerce/graphql'
3
+ import { renderHook } from '@testing-library/react'
4
+ import { describe, expect, it } from 'vitest'
5
+ import {
6
+ getPrivateQueryContext,
7
+ usePrivateQueryContext,
8
+ } from '../plugins/magentoCustomerPrivateQueryContext'
9
+ import { customerTypePolicies } from '../typePolicies'
10
+ import { CustomerTokenDocument } from './CustomerToken.gql'
11
+ import { useCustomerSession } from './useCustomerSession'
12
+
13
+ function renderWithToken<T>(valid: boolean, hook: (client: ApolloClient) => T) {
14
+ const client = new ApolloClient({
15
+ cache: new InMemoryCache({ typePolicies: customerTypePolicies }),
16
+ link: ApolloLink.empty(),
17
+ })
18
+ client.writeQuery({
19
+ query: CustomerTokenDocument,
20
+ data: {
21
+ customerToken: {
22
+ __typename: 'CustomerToken',
23
+ token: 'test-token',
24
+ createdAt: null,
25
+ valid,
26
+ xMagentoCacheId: null,
27
+ },
28
+ },
29
+ })
30
+ return renderHook(() => hook(client), {
31
+ wrapper: ({ children }) => <ApolloProvider client={client}>{children}</ApolloProvider>,
32
+ }).result.current
33
+ }
34
+
35
+ describe.each([
36
+ { valid: true, loggedIn: true },
37
+ { valid: false, loggedIn: false },
38
+ ])('a customer token with valid: $valid', ({ valid, loggedIn }) => {
39
+ it(`sets loggedIn of useCustomerSession to ${loggedIn}`, () => {
40
+ expect(renderWithToken(valid, () => useCustomerSession()).loggedIn).toBe(loggedIn)
41
+ })
42
+
43
+ it(`sets loggedIn of the private query context to ${loggedIn}`, () => {
44
+ const contexts = renderWithToken(valid, (client) => [
45
+ getPrivateQueryContext(() => null, client),
46
+ usePrivateQueryContext(() => null),
47
+ ])
48
+ expect(contexts.map((context) => context?.loggedIn ?? false)).toEqual([loggedIn, loggedIn])
49
+ })
50
+ })
@@ -16,7 +16,8 @@ export function useCustomerSession(_: UseCustomerSessionOptions = {}) {
16
16
 
17
17
  return {
18
18
  ...tokenData,
19
- loggedIn: Boolean(tokenData?.token),
19
+ // `customerLink` does not send a token with `valid: false`.
20
+ loggedIn: Boolean(tokenData?.token) && tokenData?.valid !== false,
20
21
  query,
21
22
  }
22
23
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-customer",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "11.0.0-canary.59",
5
+ "version": "11.0.0-canary.62",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -21,20 +21,20 @@
21
21
  "./plugins/magentoCustomerPrivateQueryContext": "./plugins/magentoCustomerPrivateQueryContext.ts"
22
22
  },
23
23
  "peerDependencies": {
24
- "@graphcommerce/ecommerce-ui": "^11.0.0-canary.59",
25
- "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.59",
26
- "@graphcommerce/framer-next-pages": "^11.0.0-canary.59",
27
- "@graphcommerce/framer-utils": "^11.0.0-canary.59",
28
- "@graphcommerce/graphql": "^11.0.0-canary.59",
29
- "@graphcommerce/graphql-mesh": "^11.0.0-canary.59",
30
- "@graphcommerce/image": "^11.0.0-canary.59",
31
- "@graphcommerce/magento-graphql": "^11.0.0-canary.59",
32
- "@graphcommerce/magento-store": "^11.0.0-canary.59",
33
- "@graphcommerce/next-config": "^11.0.0-canary.59",
34
- "@graphcommerce/next-ui": "^11.0.0-canary.59",
35
- "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.59",
36
- "@graphcommerce/react-hook-form": "^11.0.0-canary.59",
37
- "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.59",
24
+ "@graphcommerce/ecommerce-ui": "^11.0.0-canary.62",
25
+ "@graphcommerce/eslint-config-pwa": "^11.0.0-canary.62",
26
+ "@graphcommerce/framer-next-pages": "^11.0.0-canary.62",
27
+ "@graphcommerce/framer-utils": "^11.0.0-canary.62",
28
+ "@graphcommerce/graphql": "^11.0.0-canary.62",
29
+ "@graphcommerce/graphql-mesh": "^11.0.0-canary.62",
30
+ "@graphcommerce/image": "^11.0.0-canary.62",
31
+ "@graphcommerce/magento-graphql": "^11.0.0-canary.62",
32
+ "@graphcommerce/magento-store": "^11.0.0-canary.62",
33
+ "@graphcommerce/next-config": "^11.0.0-canary.62",
34
+ "@graphcommerce/next-ui": "^11.0.0-canary.62",
35
+ "@graphcommerce/prettier-config-pwa": "^11.0.0-canary.62",
36
+ "@graphcommerce/react-hook-form": "^11.0.0-canary.62",
37
+ "@graphcommerce/typescript-config-pwa": "^11.0.0-canary.62",
38
38
  "@lingui/core": "^6",
39
39
  "@lingui/react": "^6",
40
40
  "@mui/material": "^7.0.0",
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  useApolloClient,
3
+ type ApolloClient,
3
4
  type getPrivateQueryContextMesh as getPrivateQueryContextMeshType,
4
5
  type getPrivateQueryContext as getPrivateQueryContextType,
5
6
  type usePrivateQueryContext as usePrivateQueryContextType,
@@ -23,11 +24,17 @@ function useIsLoggedIn() {
23
24
  )
24
25
  return useSyncExternalStore(
25
26
  subscribe,
26
- () => !!cache.readQuery({ query: CustomerTokenDocument })?.customerToken?.token,
27
+ () => isLoggedIn(cache),
27
28
  () => false,
28
29
  )
29
30
  }
30
31
 
32
+ /** `customerLink` does not send a token with `valid: false`. */
33
+ function isLoggedIn(cache: ApolloClient['cache']) {
34
+ const customerToken = cache.readQuery({ query: CustomerTokenDocument })?.customerToken
35
+ return !!customerToken?.token && customerToken.valid !== false
36
+ }
37
+
31
38
  export const config: PluginConfig = {
32
39
  type: 'function',
33
40
  module: '@graphcommerce/graphql',
@@ -48,7 +55,7 @@ export const getPrivateQueryContext: FunctionPlugin<typeof getPrivateQueryContex
48
55
  client,
49
56
  ...args
50
57
  ) => {
51
- const loggedIn = !!client.cache.readQuery({ query: CustomerTokenDocument })?.customerToken?.token
58
+ const loggedIn = isLoggedIn(client.cache)
52
59
  const res = prev(client, ...args)
53
60
  if (!loggedIn) return res
54
61
  return { ...res, loggedIn: true }
@@ -6,4 +6,5 @@ extend type CustomerToken {
6
6
  createdAt: String @deprecated(reason: "Value is not used in GraphCommerce, but still filled in.")
7
7
  valid: Boolean
8
8
  xMagentoCacheId: String
9
+ email: String
9
10
  }
@@ -0,0 +1,33 @@
1
+ import { gql, InMemoryCache } from '@graphcommerce/graphql'
2
+ import { magentoTypePolicies } from '@graphcommerce/magento-graphql'
3
+ import { expect, it } from 'vitest'
4
+ import { CustomerTokenDocument } from './hooks/CustomerToken.gql'
5
+ import { customerTypePolicies, migrateCustomer } from './typePolicies'
6
+
7
+ const createCache = () =>
8
+ new InMemoryCache({ typePolicies: { ...magentoTypePolicies, ...customerTypePolicies } })
9
+
10
+ it('keeps the email of the sign-in on the customer token after a cache migration', () => {
11
+ const oldCache = createCache()
12
+ oldCache.write({
13
+ dataId: 'ROOT_MUTATION',
14
+ query: gql`
15
+ mutation SignIn($email: String!) {
16
+ generateCustomerToken(email: $email, password: "test-password") {
17
+ __typename
18
+ token
19
+ }
20
+ }
21
+ `,
22
+ variables: { email: 'customer@example.test' },
23
+ result: { generateCustomerToken: { __typename: 'CustomerToken', token: 'test-token' } },
24
+ })
25
+
26
+ const newCache = createCache()
27
+ migrateCustomer(oldCache, newCache)
28
+
29
+ expect(newCache.readQuery({ query: CustomerTokenDocument })?.customerToken).toMatchObject({
30
+ token: 'test-token',
31
+ email: 'customer@example.test',
32
+ })
33
+ })
package/typePolicies.ts CHANGED
@@ -19,6 +19,7 @@ const generateCustomerToken: FieldPolicy<Mutation['generateCustomerToken']> = {
19
19
  createdAt: new Date().toUTCString(),
20
20
  valid: true,
21
21
  xMagentoCacheId: null,
22
+ email: options.args?.email ?? null,
22
23
  },
23
24
  },
24
25
  })