From 07b22f5d4d03914b5d9889793e6e7bae7c47d21c Mon Sep 17 00:00:00 2001 From: SushantJadhav Date: Thu, 26 Aug 2021 13:15:03 +0530 Subject: [PATCH] logout Initial --- .../api/endpoints/logout/index.ts | 19 ++++++++++++- .../api/endpoints/logout/logout.ts | 23 ++++++++++++++++ framework/kibocommerce/auth/use-logout.tsx | 27 ++++++++++++------- 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 framework/kibocommerce/api/endpoints/logout/logout.ts diff --git a/framework/kibocommerce/api/endpoints/logout/index.ts b/framework/kibocommerce/api/endpoints/logout/index.ts index 491bf0ac9..ec4ded011 100644 --- a/framework/kibocommerce/api/endpoints/logout/index.ts +++ b/framework/kibocommerce/api/endpoints/logout/index.ts @@ -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 + +export type LogoutEndpoint = LogoutAPI['endpoint'] + +export const handlers: LogoutEndpoint['handlers'] = { logout } + +const logoutApi = createEndpoint({ + handler: logoutEndpoint, + handlers, +}) + +export default logoutApi diff --git a/framework/kibocommerce/api/endpoints/logout/logout.ts b/framework/kibocommerce/api/endpoints/logout/logout.ts new file mode 100644 index 000000000..b90a8c3ce --- /dev/null +++ b/framework/kibocommerce/api/endpoints/logout/logout.ts @@ -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 diff --git a/framework/kibocommerce/auth/use-logout.tsx b/framework/kibocommerce/auth/use-logout.tsx index 9b3fc3e44..e75563e04 100644 --- a/framework/kibocommerce/auth/use-logout.tsx +++ b/framework/kibocommerce/auth/use-logout.tsx @@ -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 -export const handler: MutationHook = { +export const handler: MutationHook = { 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 () => {}, }