From 1d2451550b252e2503294b20f3e0031deea491e3 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 19 May 2021 14:44:10 -0500 Subject: [PATCH] Added logout endpoint --- framework/bigcommerce/api/index.ts | 3 ++- framework/bigcommerce/api/logout/index.ts | 10 ++++++++++ framework/bigcommerce/api/logout/logout.ts | 23 ++++++++++++++++++++++ framework/bigcommerce/types/logout.ts | 1 + framework/commerce/api/index.ts | 7 ++++++- framework/commerce/types/logout.ts | 11 +++++++++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 framework/bigcommerce/api/logout/index.ts create mode 100644 framework/bigcommerce/api/logout/logout.ts create mode 100644 framework/bigcommerce/types/logout.ts create mode 100644 framework/commerce/types/logout.ts diff --git a/framework/bigcommerce/api/index.ts b/framework/bigcommerce/api/index.ts index 425759df5..609ffcfca 100644 --- a/framework/bigcommerce/api/index.ts +++ b/framework/bigcommerce/api/index.ts @@ -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

= CommerceAPI

diff --git a/framework/bigcommerce/api/logout/index.ts b/framework/bigcommerce/api/logout/index.ts new file mode 100644 index 000000000..eb658860c --- /dev/null +++ b/framework/bigcommerce/api/logout/index.ts @@ -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 + +export type LogoutEndpoint = LogoutAPI['endpoint'] + +export const operations = { logout } diff --git a/framework/bigcommerce/api/logout/logout.ts b/framework/bigcommerce/api/logout/logout.ts new file mode 100644 index 000000000..0a0673714 --- /dev/null +++ b/framework/bigcommerce/api/logout/logout.ts @@ -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 diff --git a/framework/bigcommerce/types/logout.ts b/framework/bigcommerce/types/logout.ts new file mode 100644 index 000000000..9f0a466af --- /dev/null +++ b/framework/bigcommerce/types/logout.ts @@ -0,0 +1 @@ +export * from '@commerce/types/logout' diff --git a/framework/commerce/api/index.ts b/framework/commerce/api/index.ts index 86ac17e43..f55bf9ce2 100644 --- a/framework/commerce/api/index.ts +++ b/framework/commerce/api/index.ts @@ -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, diff --git a/framework/commerce/types/logout.ts b/framework/commerce/types/logout.ts new file mode 100644 index 000000000..642095e4d --- /dev/null +++ b/framework/commerce/types/logout.ts @@ -0,0 +1,11 @@ +export type LogoutSchema = { + endpoint: { + options: {} + operations: { + logout: { + data: null + body: { redirectTo?: string } + } + } + } +}