mirror of
https://github.com/vercel/commerce.git
synced 2025-07-05 12:41:21 +00:00
logout Initial
This commit is contained in:
parent
6ae0b071b7
commit
07b22f5d4d
@ -1 +1,18 @@
|
|||||||
export default function noopApi(...args: any[]): void {}
|
import { GetAPISchema, createEndpoint } from '@commerce/api'
|
||||||
|
import logoutEndpoint from '@commerce/api/endpoints/logout'
|
||||||
|
import type { LogoutSchema } from '../../../types/logout'
|
||||||
|
import type { KiboCommerceAPI } from '../..'
|
||||||
|
import logout from './logout'
|
||||||
|
|
||||||
|
export type LogoutAPI = GetAPISchema<KiboCommerceAPI, LogoutSchema>
|
||||||
|
|
||||||
|
export type LogoutEndpoint = LogoutAPI['endpoint']
|
||||||
|
|
||||||
|
export const handlers: LogoutEndpoint['handlers'] = { logout }
|
||||||
|
|
||||||
|
const logoutApi = createEndpoint<LogoutAPI>({
|
||||||
|
handler: logoutEndpoint,
|
||||||
|
handlers,
|
||||||
|
})
|
||||||
|
|
||||||
|
export default logoutApi
|
||||||
|
23
framework/kibocommerce/api/endpoints/logout/logout.ts
Normal file
23
framework/kibocommerce/api/endpoints/logout/logout.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { serialize } from 'cookie'
|
||||||
|
import type { LogoutEndpoint } from '.'
|
||||||
|
|
||||||
|
const logout: LogoutEndpoint['handlers']['logout'] = async ({
|
||||||
|
res,
|
||||||
|
body: { redirectTo },
|
||||||
|
config,
|
||||||
|
}) => {
|
||||||
|
// Remove the cookie
|
||||||
|
res.setHeader(
|
||||||
|
'Set-Cookie',
|
||||||
|
serialize(config.customerCookie, '', { maxAge: -1, path: '/' })
|
||||||
|
)
|
||||||
|
|
||||||
|
// Only allow redirects to a relative URL
|
||||||
|
if (redirectTo?.startsWith('/')) {
|
||||||
|
res.redirect(redirectTo)
|
||||||
|
} else {
|
||||||
|
res.status(200).json({ data: null })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default logout
|
@ -1,17 +1,26 @@
|
|||||||
import { MutationHook } from '@commerce/utils/types'
|
import { useCallback } from 'react'
|
||||||
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
|
import useLogout, { UseLogout } from '@commerce/auth/use-logout'
|
||||||
|
import type { LogoutHook } from '../types/logout'
|
||||||
|
import useCustomer from '../customer/use-customer'
|
||||||
|
|
||||||
export default useLogout as UseLogout<typeof handler>
|
export default useLogout as UseLogout<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<LogoutHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '/api/logout',
|
||||||
|
method: 'GET',
|
||||||
},
|
},
|
||||||
async fetcher() {
|
useHook: ({ fetch }) => () => {
|
||||||
return null
|
const { mutate } = useCustomer()
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function logout() {
|
||||||
|
const data = await fetch()
|
||||||
|
await mutate(null, false)
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
[fetch, mutate]
|
||||||
|
)
|
||||||
},
|
},
|
||||||
useHook:
|
|
||||||
({ fetch }) =>
|
|
||||||
() =>
|
|
||||||
async () => {},
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user