logout Initial

This commit is contained in:
SushantJadhav 2021-08-26 13:15:03 +05:30
parent 6ae0b071b7
commit 07b22f5d4d
3 changed files with 59 additions and 10 deletions

View File

@ -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

View 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

View File

@ -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 type { LogoutHook } from '../types/logout'
import useCustomer from '../customer/use-customer'
export default useLogout as UseLogout<typeof handler>
export const handler: MutationHook<any> = {
export const handler: MutationHook<LogoutHook> = {
fetchOptions: {
query: '',
url: '/api/logout',
method: 'GET',
},
async fetcher() {
return null
useHook: ({ fetch }) => () => {
const { mutate } = useCustomer()
return useCallback(
async function logout() {
const data = await fetch()
await mutate(null, false)
return data
},
[fetch, mutate]
)
},
useHook:
({ fetch }) =>
() =>
async () => {},
}