feat: disable Wishlist

chore: setup commerce & next config
fix: replace all call to bigcommerce from aquilacms provider
feat add validation to input in signup
This commit is contained in:
Gérard Le Cloerec
2021-04-06 15:08:19 +02:00
parent 94861a922a
commit a5de31ae17
68 changed files with 2157 additions and 7640 deletions

View File

@@ -1,28 +1,8 @@
import type { GetLoggedInCustomerQuery } from '../../../schema'
import type { CustomersHandlers } from '..'
import { normalizeUser } from '../../../lib/normalize'
import type { AquilacmsUser, User } from '../../../types'
export const getLoggedInCustomerQuery = /* GraphQL */ `
query getLoggedInCustomer {
customer {
entityId
firstName
lastName
email
company
customerGroupId
notes
phone
addressCount
attributeCount
storeCredit {
value
currencyCode
}
}
}
`
export type Customer = NonNullable<GetLoggedInCustomerQuery['customer']>
export type Customer = User
const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({
req,
@@ -32,25 +12,27 @@ const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({
const token = req.cookies[config.customerCookie]
if (token) {
const { data } = await config.fetch<GetLoggedInCustomerQuery>(
getLoggedInCustomerQuery,
undefined,
{
try {
const data = await config.storeApiFetch('/v2/user', {
method: 'POST',
body: JSON.stringify({
PostBody: {},
}),
headers: {
cookie: `${config.customerCookie}=${token}`,
authorization: token,
},
}
)
const { customer } = data
if (!customer) {
return res.status(400).json({
data: null,
errors: [{ message: 'Customer not found', code: 'not_found' }],
})
if (!data) {
return res.status(400).json({
data: null,
errors: [{ message: 'Customer not found', code: 'not_found' }],
})
}
const customer = normalizeUser(data as AquilacmsUser)
return res.status(200).json({ data: { customer } })
} catch (err) {
console.error(err)
}
return res.status(200).json({ data: { customer } })
}
res.status(200).json({ data: null })

View File

@@ -1,4 +1,5 @@
import { serialize } from 'cookie'
import { getConfig } from '../..'
import { LogoutHandlers } from '../logout'
const logoutHandler: LogoutHandlers['logout'] = async ({
@@ -6,6 +7,12 @@ const logoutHandler: LogoutHandlers['logout'] = async ({
body: { redirectTo },
config,
}) => {
config = getConfig(config)
await config.storeApiFetch('/v2/auth/logout', {
method: 'GET',
})
// Remove the cookie
res.setHeader(
'Set-Cookie',

View File

@@ -1,36 +1,49 @@
import { AquilacmsApiError } from '../../utils/errors'
import login from '../../../auth/login'
import { SignupHandlers } from '../signup'
import Joi, { valid } from 'joi'
const signup: SignupHandlers['signup'] = async ({
res,
body: { firstName, lastName, email, password },
config,
}) => {
// TODO: Add proper validations with something like Ajv
const schema = Joi.object({
firstName: Joi.string().min(1),
lastName: Joi.string().min(1),
email: Joi.string().email({ tlds: false }),
password: Joi.string().pattern(
new RegExp('^(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^wd:])([^s]){6,}$')
),
})
const validation = schema.validate({ email, password })
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.
if (validation?.error?.details) {
let message = validation.error.details[0].message
if (validation.error.details[0].path[0] === 'password')
message =
'password must have a lowercase, a uppercase, a number and be at least 6 characters long'
return res.status(400).json({
data: null,
errors: [{ message }],
})
}
try {
await config.storeApiFetch('/v3/customers', {
method: 'POST',
body: JSON.stringify([
{
first_name: firstName,
last_name: lastName,
email,
authentication: {
new_password: password,
},
},
]),
await config.storeApiFetch('/v2/user', {
method: 'PUT',
body: JSON.stringify({
firstname: firstName,
lastname: lastName,
email,
password,
}),
})
} catch (error) {
if (error instanceof AquilacmsApiError && error.status === 422) {