Added signup endpoint

This commit is contained in:
Luis Alvarez
2021-05-20 13:44:31 -05:00
parent f6ec88a817
commit 7d7e4342be
8 changed files with 145 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ import type { CartAPI } from './cart'
import type { CustomerAPI } from './customer'
import type { LoginAPI } from './login'
import type { LogoutAPI } from './logout'
import type { SignupAPI } from './signup'
import login from './operations/login'
export interface BigcommerceConfig extends CommerceAPIConfig {
@@ -114,7 +115,7 @@ export const provider = {
export type Provider = typeof provider
export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI
export type APIs = CartAPI | CustomerAPI | LoginAPI | LogoutAPI | SignupAPI
export type BigcommerceAPI<P extends Provider = Provider> = CommerceAPI<P>

View File

@@ -0,0 +1,10 @@
import type { GetAPISchema } from '@commerce/api'
import type { SignupSchema } from '../../types/signup'
import type { BigcommerceAPI } from '..'
import signup from './signup'
export type SignupAPI = GetAPISchema<BigcommerceAPI, SignupSchema>
export type SignupEndpoint = SignupAPI['endpoint']
export const operations = { signup }

View File

@@ -0,0 +1,62 @@
import { BigcommerceApiError } from '../utils/errors'
import type { SignupEndpoint } from '.'
const signup: SignupEndpoint['operations']['signup'] = async ({
res,
body: { firstName, lastName, email, password },
config,
commerce,
}) => {
// 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 commerce.login({ variables: { email, password }, res, config })
res.status(200).json({ data: null })
}
export default signup

View File

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