Added logout endpoint

This commit is contained in:
Luis Alvarez 2021-05-19 14:44:10 -05:00
parent 1ba6049991
commit 1d2451550b
6 changed files with 53 additions and 2 deletions

View File

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

View 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 }

View 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

View File

@ -0,0 +1 @@
export * from '@commerce/types/logout'

View File

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

View File

@ -0,0 +1,11 @@
export type LogoutSchema = {
endpoint: {
options: {}
operations: {
logout: {
data: null
body: { redirectTo?: string }
}
}
}
}