@graphcommerce/magento-customer 6.2.0-canary.29 → 6.2.0-canary.30
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 +6 -0
- package/components/CustomerFab/CustomerFab.tsx +3 -0
- package/hooks/UseCustomerValidateToken.graphql +5 -0
- package/hooks/useCustomerValidateToken.ts +23 -0
- package/link/createCustomerTokenLink.ts +2 -7
- package/package.json +13 -13
- package/link/onAuthenticationError.ts +0 -39
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 6.2.0-canary.30
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1942](https://github.com/graphcommerce-org/graphcommerce/pull/1942) [`21b0d0c48`](https://github.com/graphcommerce-org/graphcommerce/commit/21b0d0c48603343c09f287978bf051140e9be912) - Customer's session is now revalidated when a previous session is detected on pageload, making sure the customer is still logged in. ([@paales](https://github.com/paales))
|
|
8
|
+
|
|
3
9
|
## 6.2.0-canary.29
|
|
4
10
|
|
|
5
11
|
## 6.2.0-canary.28
|
|
@@ -8,6 +8,7 @@ import { i18n } from '@lingui/core'
|
|
|
8
8
|
import { Fab, FabProps as FabPropsType, NoSsr, SxProps, Theme } from '@mui/material'
|
|
9
9
|
import React from 'react'
|
|
10
10
|
import { useCustomerSession, UseCustomerSessionReturn } from '../../hooks'
|
|
11
|
+
import { useCustomerValidateToken } from '../../hooks/useCustomerValidateToken'
|
|
11
12
|
|
|
12
13
|
type CustomerFabContentProps = {
|
|
13
14
|
icon?: React.ReactNode
|
|
@@ -53,6 +54,8 @@ export type CustomerFabProps = Omit<CustomerFabContentProps, 'session'>
|
|
|
53
54
|
export function CustomerFab(props: CustomerFabProps) {
|
|
54
55
|
const session = useCustomerSession()
|
|
55
56
|
|
|
57
|
+
useCustomerValidateToken()
|
|
58
|
+
|
|
56
59
|
return (
|
|
57
60
|
<NoSsr fallback={<CustomerFabContent {...props} />}>
|
|
58
61
|
<CustomerFabContent session={session} {...props} />
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useApolloClient } from '@graphcommerce/graphql'
|
|
2
|
+
import { ErrorCategory } from '@graphcommerce/magento-graphql'
|
|
3
|
+
import { useRouter } from 'next/router'
|
|
4
|
+
import { UseCustomerValidateTokenDocument } from './UseCustomerValidateToken.gql'
|
|
5
|
+
import { useCustomerQuery } from './useCustomerQuery'
|
|
6
|
+
|
|
7
|
+
/** Validates the current customer token. This hook is supposed to be called only once. */
|
|
8
|
+
export function useCustomerValidateToken() {
|
|
9
|
+
const client = useApolloClient()
|
|
10
|
+
const router = useRouter()
|
|
11
|
+
|
|
12
|
+
useCustomerQuery(UseCustomerValidateTokenDocument, {
|
|
13
|
+
initialFetchPolicy: 'network-only',
|
|
14
|
+
onError: async ({ graphQLErrors }) => {
|
|
15
|
+
const category: ErrorCategory = 'graphql-authorization'
|
|
16
|
+
// If there is no authorization error, do nothing.
|
|
17
|
+
if (!graphQLErrors.some((e) => e.extensions?.category === category)) return
|
|
18
|
+
|
|
19
|
+
await client.clearStore()
|
|
20
|
+
router.reload()
|
|
21
|
+
},
|
|
22
|
+
})
|
|
23
|
+
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ApolloLink, ApolloCache, setContext } from '@graphcommerce/graphql/apollo'
|
|
1
|
+
import { setContext } from '@graphcommerce/graphql/apollo'
|
|
3
2
|
import { CustomerTokenDocument } from '../hooks'
|
|
4
|
-
import { onAuthorizationError } from './onAuthenticationError'
|
|
5
3
|
|
|
6
4
|
export const addTokenHeader = setContext((_, context) => {
|
|
7
5
|
if (!context.headers) context.headers = {}
|
|
@@ -17,7 +15,4 @@ export const addTokenHeader = setContext((_, context) => {
|
|
|
17
15
|
}
|
|
18
16
|
})
|
|
19
17
|
|
|
20
|
-
export const customerTokenLink =
|
|
21
|
-
|
|
22
|
-
/** Not really required anymore, you can use customerTokenLink directly */
|
|
23
|
-
export const createCustomerTokenLink = (_: ApolloCache<NormalizedCacheObject>) => customerTokenLink
|
|
18
|
+
export const customerTokenLink = addTokenHeader
|
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": "6.2.0-canary.
|
|
5
|
+
"version": "6.2.0-canary.30",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -12,20 +12,20 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@graphcommerce/eslint-config-pwa": "6.2.0-canary.
|
|
16
|
-
"@graphcommerce/prettier-config-pwa": "6.2.0-canary.
|
|
17
|
-
"@graphcommerce/typescript-config-pwa": "6.2.0-canary.
|
|
15
|
+
"@graphcommerce/eslint-config-pwa": "6.2.0-canary.30",
|
|
16
|
+
"@graphcommerce/prettier-config-pwa": "6.2.0-canary.30",
|
|
17
|
+
"@graphcommerce/typescript-config-pwa": "6.2.0-canary.30"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@graphcommerce/ecommerce-ui": "6.2.0-canary.
|
|
21
|
-
"@graphcommerce/framer-utils": "6.2.0-canary.
|
|
22
|
-
"@graphcommerce/graphql": "6.2.0-canary.
|
|
23
|
-
"@graphcommerce/graphql-mesh": "6.2.0-canary.
|
|
24
|
-
"@graphcommerce/image": "6.2.0-canary.
|
|
25
|
-
"@graphcommerce/magento-graphql": "6.2.0-canary.
|
|
26
|
-
"@graphcommerce/magento-store": "6.2.0-canary.
|
|
27
|
-
"@graphcommerce/next-ui": "6.2.0-canary.
|
|
28
|
-
"@graphcommerce/react-hook-form": "6.2.0-canary.
|
|
20
|
+
"@graphcommerce/ecommerce-ui": "6.2.0-canary.30",
|
|
21
|
+
"@graphcommerce/framer-utils": "6.2.0-canary.30",
|
|
22
|
+
"@graphcommerce/graphql": "6.2.0-canary.30",
|
|
23
|
+
"@graphcommerce/graphql-mesh": "6.2.0-canary.30",
|
|
24
|
+
"@graphcommerce/image": "6.2.0-canary.30",
|
|
25
|
+
"@graphcommerce/magento-graphql": "6.2.0-canary.30",
|
|
26
|
+
"@graphcommerce/magento-store": "6.2.0-canary.30",
|
|
27
|
+
"@graphcommerce/next-ui": "6.2.0-canary.30",
|
|
28
|
+
"@graphcommerce/react-hook-form": "6.2.0-canary.30"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@lingui/react": "^3.13.2",
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { InMemoryCache } from '@graphcommerce/graphql'
|
|
2
|
-
import { onError } from '@graphcommerce/graphql/apollo'
|
|
3
|
-
import { CustomerTokenDocument } from '../hooks/CustomerToken.gql'
|
|
4
|
-
|
|
5
|
-
function invalidateToken(cache: InMemoryCache) {
|
|
6
|
-
const res = cache.readQuery({
|
|
7
|
-
query: CustomerTokenDocument,
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
if (res?.customerToken?.valid) {
|
|
11
|
-
// Write arbitrary old token to document
|
|
12
|
-
cache.writeQuery({
|
|
13
|
-
query: CustomerTokenDocument,
|
|
14
|
-
data: {
|
|
15
|
-
customerToken: {
|
|
16
|
-
...res.customerToken,
|
|
17
|
-
token: null,
|
|
18
|
-
createdAt: new Date('2000').toUTCString(),
|
|
19
|
-
valid: false,
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
broadcast: true,
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const onAuthorizationError = onError(({ graphQLErrors, operation }) => {
|
|
28
|
-
const { cache } = operation.getContext()
|
|
29
|
-
if (graphQLErrors) {
|
|
30
|
-
for (const err of graphQLErrors) {
|
|
31
|
-
const isCustomerError = err.path?.join('.').toLowerCase().includes('customer')
|
|
32
|
-
if (err.extensions?.category === 'graphql-authorization' && isCustomerError) {
|
|
33
|
-
// Modify the operation context with a new token
|
|
34
|
-
invalidateToken(cache as InMemoryCache)
|
|
35
|
-
break
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
})
|