mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 06:56:59 +00:00
add logout hook
This commit is contained in:
parent
8a0395050f
commit
5b29bf8c78
@ -1,17 +1,24 @@
|
|||||||
import { MutationHook } from '@vercel/commerce/utils/types'
|
import { MutationHook } from '@vercel/commerce/utils/types'
|
||||||
import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
|
import useLogout, { UseLogout } from '@vercel/commerce/auth/use-logout'
|
||||||
|
import { useCustomer } from '../customer'
|
||||||
|
import { useCallback } from 'react'
|
||||||
|
import { setCustomerToken } from '../utils/token/customer-token'
|
||||||
|
|
||||||
export default useLogout as UseLogout<typeof handler>
|
export default useLogout as UseLogout<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<any> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '',
|
||||||
},
|
},
|
||||||
async fetcher() {
|
useHook: () => () => {
|
||||||
return null
|
const { mutate } = useCustomer()
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function logout() {
|
||||||
|
setCustomerToken(null)
|
||||||
|
await mutate()
|
||||||
|
},
|
||||||
|
[mutate]
|
||||||
|
)
|
||||||
},
|
},
|
||||||
useHook:
|
|
||||||
({ fetch }) =>
|
|
||||||
() =>
|
|
||||||
async () => {},
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import useCustomer, {
|
|||||||
} from '@vercel/commerce/customer/use-customer'
|
} from '@vercel/commerce/customer/use-customer'
|
||||||
import { getCustomerRoute } from '../utils/token/customer-route'
|
import { getCustomerRoute } from '../utils/token/customer-route'
|
||||||
import { normalizeCustomer } from '../utils/normalize/normalize-customer'
|
import { normalizeCustomer } from '../utils/normalize/normalize-customer'
|
||||||
|
import { getCustomerToken } from '../utils/token/customer-token'
|
||||||
import { CustomerHook } from '@vercel/commerce/types/customer'
|
import { CustomerHook } from '@vercel/commerce/types/customer'
|
||||||
|
|
||||||
export default useCustomer as UseCustomer<typeof handler>
|
export default useCustomer as UseCustomer<typeof handler>
|
||||||
@ -14,12 +15,14 @@ export const handler: SWRHook<CustomerHook> = {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
},
|
},
|
||||||
fetcher: async ({ options, fetch }) => {
|
fetcher: async ({ options, fetch }) => {
|
||||||
|
if (getCustomerToken()) {
|
||||||
const syliusCustomer = await fetch({
|
const syliusCustomer = await fetch({
|
||||||
url: getCustomerRoute() ?? '',
|
url: getCustomerRoute() ?? '',
|
||||||
method: options.method,
|
method: options.method,
|
||||||
})
|
})
|
||||||
const customer = normalizeCustomer(syliusCustomer)
|
return normalizeCustomer(syliusCustomer)
|
||||||
return customer
|
}
|
||||||
|
return null
|
||||||
},
|
},
|
||||||
useHook:
|
useHook:
|
||||||
({ useData }) =>
|
({ useData }) =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user