commerce/framework/aquilacms/api/customers/handlers/get-logged-in-customer.ts
Gérard Le Cloerec 3f41969f5c feat: disable Wishlist
chore: setup commerce & next config
fix: replace all call to bigcommerce from aquilacms provider
feat add validation to input in signup
2021-04-06 15:08:19 +02:00

42 lines
1020 B
TypeScript

import type { CustomersHandlers } from '..'
import { normalizeUser } from '../../../lib/normalize'
import type { AquilacmsUser, User } from '../../../types'
export type Customer = User
const getLoggedInCustomer: CustomersHandlers['getLoggedInCustomer'] = async ({
req,
res,
config,
}) => {
const token = req.cookies[config.customerCookie]
if (token) {
try {
const data = await config.storeApiFetch('/v2/user', {
method: 'POST',
body: JSON.stringify({
PostBody: {},
}),
headers: {
authorization: token,
},
})
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)
}
}
res.status(200).json({ data: null })
}
export default getLoggedInCustomer