mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 05:31:22 +00:00
Added logout endpoint
This commit is contained in:
parent
1ba6049991
commit
1d2451550b
@ -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'
|
@ -4,6 +4,7 @@ import type { APIEndpoint, APIHandler } from './utils/types'
|
||||
import type { CartSchema } from '../types/cart'
|
||||
import type { CustomerSchema } from '../types/customer'
|
||||
import type { LoginSchema } from '../types/login'
|
||||
import type { LogoutSchema } from '../types/logout'
|
||||
import {
|
||||
defaultOperations,
|
||||
OPERATIONS,
|
||||
@ -11,7 +12,11 @@ import {
|
||||
APIOperations,
|
||||
} from './operations'
|
||||
|
||||
export type APISchemas = CartSchema | CustomerSchema | LoginSchema
|
||||
export type APISchemas =
|
||||
| CartSchema
|
||||
| CustomerSchema
|
||||
| LoginSchema
|
||||
| LogoutSchema
|
||||
|
||||
export type GetAPISchema<
|
||||
C extends CommerceAPI<any>,
|
||||
|
11
framework/commerce/types/logout.ts
Normal file
11
framework/commerce/types/logout.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export type LogoutSchema = {
|
||||
endpoint: {
|
||||
options: {}
|
||||
operations: {
|
||||
logout: {
|
||||
data: null
|
||||
body: { redirectTo?: string }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user