mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 11:11:24 +00:00
Added logout endpoint
This commit is contained in:
@@ -12,6 +12,7 @@ import fetchStoreApi from './utils/fetch-store-api'
|
||||
import type { CartAPI } from './cart'
|
||||
import type { CustomerAPI } from './customer'
|
||||
import type { LoginAPI } from './login'
|
||||
import type { LogoutAPI } from './logout'
|
||||
import login from './operations/login'
|
||||
|
||||
export interface BigcommerceConfig extends CommerceAPIConfig {
|
||||
@@ -113,7 +114,7 @@ export const provider = {
|
||||
|
||||
export type Provider = typeof provider
|
||||
|
||||
export type APIs = CartAPI | CustomerAPI | LoginAPI
|
||||
export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI
|
||||
|
||||
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>
|
||||
|
||||
|
10
framework/bigcommerce/api/logout/index.ts
Normal file
10
framework/bigcommerce/api/logout/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { GetAPISchema } from '@commerce/api'
|
||||
import type { LogoutSchema } from '../../types/logout'
|
||||
import type { BigcommerceAPI } from '..'
|
||||
import logout from './logout'
|
||||
|
||||
export type LogoutAPI = GetAPISchema<BigcommerceAPI, LogoutSchema>
|
||||
|
||||
export type LogoutEndpoint = LogoutAPI['endpoint']
|
||||
|
||||
export const operations = { logout }
|
23
framework/bigcommerce/api/logout/logout.ts
Normal file
23
framework/bigcommerce/api/logout/logout.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { serialize } from 'cookie'
|
||||
import type { LogoutEndpoint } from '.'
|
||||
|
||||
const logout: LogoutEndpoint['operations']['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
framework/bigcommerce/types/logout.ts
Normal file
1
framework/bigcommerce/types/logout.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from '@commerce/types/logout'
|
Reference in New Issue
Block a user