diff --git a/framework/bigcommerce/api/customers/handlers/get-logged-in-customer.ts b/framework/bigcommerce/api/customers/handlers/get-logged-in-customer.ts deleted file mode 100644 index 698235dda..000000000 --- a/framework/bigcommerce/api/customers/handlers/get-logged-in-customer.ts +++ /dev/null @@ -1,59 +0,0 @@ -import type { GetLoggedInCustomerQuery } from '../../../schema' -import type { CustomersHandlers } from '..' - -export const getLoggedInCustomerQuery = /* GraphQL */ ` - query getLoggedInCustomer { - customer { - entityId - firstName - lastName - email - company - customerGroupId - notes - phone - addressCount - attributeCount - storeCredit { - value - currencyCode - } - } - } -` - -export type Customer = NonNullable - -const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({ - req, - res, - config, -}) => { - const token = req.cookies[config.customerCookie] - - if (token) { - const { data } = await config.fetch( - getLoggedInCustomerQuery, - undefined, - { - headers: { - cookie: `${config.customerCookie}=${token}`, - }, - } - ) - const { customer } = data - - if (!customer) { - return res.status(400).json({ - data: null, - errors: [{ message: 'Customer not found', code: 'not_found' }], - }) - } - - return res.status(200).json({ data: { customer } }) - } - - res.status(200).json({ data: null }) -} - -export default getLoggedInCustomer diff --git a/framework/bigcommerce/api/customers/handlers/login.ts b/framework/bigcommerce/api/customers/handlers/login.ts deleted file mode 100644 index 9e019f3a0..000000000 --- a/framework/bigcommerce/api/customers/handlers/login.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { FetcherError } from '@commerce/utils/errors' -import login from '../../../auth/login' -import type { LoginHandlers } from '../login' - -const invalidCredentials = /invalid credentials/i - -const loginHandler: LoginHandlers['login'] = async ({ - res, - body: { email, password }, - config, -}) => { - // TODO: Add proper validations with something like Ajv - if (!(email && password)) { - return res.status(400).json({ - data: null, - errors: [{ message: 'Invalid request' }], - }) - } - // TODO: validate the password and email - // Passwords must be at least 7 characters and contain both alphabetic - // and numeric characters. - - try { - await login({ variables: { email, password }, config, res }) - } catch (error) { - // Check if the email and password didn't match an existing account - if ( - error instanceof FetcherError && - invalidCredentials.test(error.message) - ) { - return res.status(401).json({ - data: null, - errors: [ - { - message: - 'Cannot find an account that matches the provided credentials', - code: 'invalid_credentials', - }, - ], - }) - } - - throw error - } - - res.status(200).json({ data: null }) -} - -export default loginHandler diff --git a/framework/bigcommerce/api/customers/handlers/logout.ts b/framework/bigcommerce/api/customers/handlers/logout.ts deleted file mode 100644 index 937ce0954..000000000 --- a/framework/bigcommerce/api/customers/handlers/logout.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { serialize } from 'cookie' -import { LogoutHandlers } from '../logout' - -const logoutHandler: LogoutHandlers['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 logoutHandler diff --git a/framework/bigcommerce/api/customers/handlers/signup.ts b/framework/bigcommerce/api/customers/handlers/signup.ts deleted file mode 100644 index 1b24db0cc..000000000 --- a/framework/bigcommerce/api/customers/handlers/signup.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { BigcommerceApiError } from '../../utils/errors' -import login from '../../../auth/login' -import { SignupHandlers } from '../signup' - -const signup: SignupHandlers['signup'] = async ({ - res, - body: { firstName, lastName, email, password }, - config, -}) => { - // TODO: Add proper validations with something like Ajv - if (!(firstName && lastName && email && password)) { - return res.status(400).json({ - data: null, - errors: [{ message: 'Invalid request' }], - }) - } - // TODO: validate the password and email - // Passwords must be at least 7 characters and contain both alphabetic - // and numeric characters. - - try { - await config.storeApiFetch('/v3/customers', { - method: 'POST', - body: JSON.stringify([ - { - first_name: firstName, - last_name: lastName, - email, - authentication: { - new_password: password, - }, - }, - ]), - }) - } catch (error) { - if (error instanceof BigcommerceApiError && error.status === 422) { - const hasEmailError = '0.email' in error.data?.errors - - // If there's an error with the email, it most likely means it's duplicated - if (hasEmailError) { - return res.status(400).json({ - data: null, - errors: [ - { - message: 'The email is already in use', - code: 'duplicated_email', - }, - ], - }) - } - } - - throw error - } - - // Login the customer right after creating it - await login({ variables: { email, password }, res, config }) - - res.status(200).json({ data: null }) -} - -export default signup diff --git a/framework/bigcommerce/api/customers/index.ts b/framework/bigcommerce/api/customers/index.ts deleted file mode 100644 index 5af4d1d1d..000000000 --- a/framework/bigcommerce/api/customers/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import createApiHandler, { - BigcommerceApiHandler, - BigcommerceHandler, -} from '../utils/create-api-handler' -import isAllowedMethod from '../utils/is-allowed-method' -import { BigcommerceApiError } from '../utils/errors' -import getLoggedInCustomer, { - Customer, -} from './handlers/get-logged-in-customer' - -export type { Customer } - -export type CustomerData = { - customer: Customer -} - -export type CustomersHandlers = { - getLoggedInCustomer: BigcommerceHandler -} - -const METHODS = ['GET'] - -const customersApi: BigcommerceApiHandler< - CustomerData, - CustomersHandlers -> = async (req, res, config, handlers) => { - if (!isAllowedMethod(req, res, METHODS)) return - - try { - const body = null - return await handlers['getLoggedInCustomer']({ req, res, config, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -const handlers = { getLoggedInCustomer } - -export default createApiHandler(customersApi, handlers, {}) diff --git a/framework/bigcommerce/api/customers/login.ts b/framework/bigcommerce/api/customers/login.ts deleted file mode 100644 index e8f24a92d..000000000 --- a/framework/bigcommerce/api/customers/login.ts +++ /dev/null @@ -1,45 +0,0 @@ -import createApiHandler, { - BigcommerceApiHandler, - BigcommerceHandler, -} from '../utils/create-api-handler' -import isAllowedMethod from '../utils/is-allowed-method' -import { BigcommerceApiError } from '../utils/errors' -import login from './handlers/login' - -export type LoginBody = { - email: string - password: string -} - -export type LoginHandlers = { - login: BigcommerceHandler> -} - -const METHODS = ['POST'] - -const loginApi: BigcommerceApiHandler = async ( - req, - res, - config, - handlers -) => { - if (!isAllowedMethod(req, res, METHODS)) return - - try { - const body = req.body ?? {} - return await handlers['login']({ req, res, config, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -const handlers = { login } - -export default createApiHandler(loginApi, handlers, {}) diff --git a/framework/bigcommerce/api/customers/logout.ts b/framework/bigcommerce/api/customers/logout.ts deleted file mode 100644 index 0a4965245..000000000 --- a/framework/bigcommerce/api/customers/logout.ts +++ /dev/null @@ -1,42 +0,0 @@ -import createApiHandler, { - BigcommerceApiHandler, - BigcommerceHandler, -} from '../utils/create-api-handler' -import isAllowedMethod from '../utils/is-allowed-method' -import { BigcommerceApiError } from '../utils/errors' -import logout from './handlers/logout' - -export type LogoutHandlers = { - logout: BigcommerceHandler -} - -const METHODS = ['GET'] - -const logoutApi: BigcommerceApiHandler = async ( - req, - res, - config, - handlers -) => { - if (!isAllowedMethod(req, res, METHODS)) return - - try { - const redirectTo = req.query.redirect_to - const body = typeof redirectTo === 'string' ? { redirectTo } : {} - - return await handlers['logout']({ req, res, config, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -const handlers = { logout } - -export default createApiHandler(logoutApi, handlers, {}) diff --git a/framework/bigcommerce/api/customers/signup.ts b/framework/bigcommerce/api/customers/signup.ts deleted file mode 100644 index aa26f78cf..000000000 --- a/framework/bigcommerce/api/customers/signup.ts +++ /dev/null @@ -1,50 +0,0 @@ -import createApiHandler, { - BigcommerceApiHandler, - BigcommerceHandler, -} from '../utils/create-api-handler' -import isAllowedMethod from '../utils/is-allowed-method' -import { BigcommerceApiError } from '../utils/errors' -import signup from './handlers/signup' - -export type SignupBody = { - firstName: string - lastName: string - email: string - password: string -} - -export type SignupHandlers = { - signup: BigcommerceHandler> -} - -const METHODS = ['POST'] - -const signupApi: BigcommerceApiHandler = async ( - req, - res, - config, - handlers -) => { - if (!isAllowedMethod(req, res, METHODS)) return - - const { cookies } = req - const cartId = cookies[config.cartCookie] - - try { - const body = { ...req.body, cartId } - return await handlers['signup']({ req, res, config, body }) - } catch (error) { - console.error(error) - - const message = - error instanceof BigcommerceApiError - ? 'An unexpected error ocurred with the Bigcommerce API' - : 'An unexpected error ocurred' - - res.status(500).json({ data: null, errors: [{ message }] }) - } -} - -const handlers = { signup } - -export default createApiHandler(signupApi, handlers, {})