mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 11:11:24 +00:00
Added signup endpoint
This commit is contained in:
@@ -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>
|
||||
|
||||
|
10
framework/bigcommerce/api/signup/index.ts
Normal file
10
framework/bigcommerce/api/signup/index.ts
Normal 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 }
|
62
framework/bigcommerce/api/signup/signup.ts
Normal file
62
framework/bigcommerce/api/signup/signup.ts
Normal 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
|
1
framework/bigcommerce/types/signup.ts
Normal file
1
framework/bigcommerce/types/signup.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from '@commerce/types/signup'
|
Reference in New Issue
Block a user