forked from crowetic/commerce
Added useLogin hook and API endpoint
This commit is contained in:
parent
e2b26715b9
commit
a1e47a73d2
@ -57,6 +57,7 @@ const createCustomer: CustomersHandlers['createCustomer'] = async ({
|
|||||||
|
|
||||||
console.log('DATA', result.data)
|
console.log('DATA', result.data)
|
||||||
|
|
||||||
|
// TODO: Currently not working, fix this asap.
|
||||||
const loginData = await login({ variables: { email, password }, config })
|
const loginData = await login({ variables: { email, password }, config })
|
||||||
|
|
||||||
console.log('LOGIN DATA', loginData)
|
console.log('LOGIN DATA', loginData)
|
||||||
|
26
lib/bigcommerce/api/customers/handlers/login.ts
Normal file
26
lib/bigcommerce/api/customers/handlers/login.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import login from '../../operations/login'
|
||||||
|
import type { LoginHandlers } from '../login'
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
// TODO: Currently not working, fix this asap.
|
||||||
|
const loginData = await login({ variables: { email, password }, config })
|
||||||
|
|
||||||
|
res.status(200).json({ data: null })
|
||||||
|
}
|
||||||
|
|
||||||
|
export default loginHandler
|
45
lib/bigcommerce/api/customers/login.ts
Normal file
45
lib/bigcommerce/api/customers/login.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
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<null, Partial<LoginBody>>
|
||||||
|
}
|
||||||
|
|
||||||
|
const METHODS = ['POST']
|
||||||
|
|
||||||
|
const loginApi: BigcommerceApiHandler<null, LoginHandlers> = 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, {})
|
@ -3,7 +3,7 @@ import { BigcommerceConfig, getConfig } from '..'
|
|||||||
|
|
||||||
export type BigcommerceApiHandler<
|
export type BigcommerceApiHandler<
|
||||||
T = any,
|
T = any,
|
||||||
H extends BigcommerceHandlers = {},
|
H extends BigcommerceHandlers<T> = {},
|
||||||
Options extends {} = {}
|
Options extends {} = {}
|
||||||
> = (
|
> = (
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
|
49
lib/bigcommerce/use-login.tsx
Normal file
49
lib/bigcommerce/use-login.tsx
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import { useCallback } from 'react'
|
||||||
|
import { HookFetcher } from '@lib/commerce/utils/types'
|
||||||
|
import useCommerceLogin from '@lib/commerce/use-login'
|
||||||
|
import type { LoginBody } from './api/customers/login'
|
||||||
|
|
||||||
|
const defaultOpts = {
|
||||||
|
url: '/api/bigcommerce/customers/login',
|
||||||
|
method: 'POST',
|
||||||
|
}
|
||||||
|
|
||||||
|
export type LoginInput = LoginBody
|
||||||
|
|
||||||
|
export const fetcher: HookFetcher<null, LoginBody> = (
|
||||||
|
options,
|
||||||
|
{ email, password },
|
||||||
|
fetch
|
||||||
|
) => {
|
||||||
|
if (!(email && password)) {
|
||||||
|
throw new Error(
|
||||||
|
'A first name, last name, email and password are required to login'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch({
|
||||||
|
url: options?.url ?? defaultOpts.url,
|
||||||
|
method: options?.method ?? defaultOpts.method,
|
||||||
|
body: { email, password },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
|
const useLogin = () => {
|
||||||
|
const fn = useCommerceLogin<null, LoginInput>(defaultOpts, customFetcher)
|
||||||
|
|
||||||
|
return useCallback(
|
||||||
|
async function login(input: LoginInput) {
|
||||||
|
const data = await fn(input)
|
||||||
|
return data
|
||||||
|
},
|
||||||
|
[fn]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
useLogin.extend = extendHook
|
||||||
|
|
||||||
|
return useLogin
|
||||||
|
}
|
||||||
|
|
||||||
|
export default extendHook(fetcher)
|
@ -10,7 +10,7 @@ const defaultOpts = {
|
|||||||
|
|
||||||
export type SignupInput = CreateCustomerBody
|
export type SignupInput = CreateCustomerBody
|
||||||
|
|
||||||
export const fetcher: HookFetcher<undefined, CreateCustomerBody> = (
|
export const fetcher: HookFetcher<null, CreateCustomerBody> = (
|
||||||
options,
|
options,
|
||||||
{ firstName, lastName, email, password },
|
{ firstName, lastName, email, password },
|
||||||
fetch
|
fetch
|
||||||
@ -30,10 +30,7 @@ export const fetcher: HookFetcher<undefined, CreateCustomerBody> = (
|
|||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
export function extendHook(customFetcher: typeof fetcher) {
|
||||||
const useSignup = () => {
|
const useSignup = () => {
|
||||||
const fn = useCommerceSignup<undefined, SignupInput>(
|
const fn = useCommerceSignup<null, SignupInput>(defaultOpts, customFetcher)
|
||||||
defaultOpts,
|
|
||||||
customFetcher
|
|
||||||
)
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function signup(input: SignupInput) {
|
async function signup(input: SignupInput) {
|
||||||
|
5
lib/commerce/use-login.tsx
Normal file
5
lib/commerce/use-login.tsx
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import useAction from './utils/use-action'
|
||||||
|
|
||||||
|
const useLogin = useAction
|
||||||
|
|
||||||
|
export default useLogin
|
3
pages/api/bigcommerce/customers/login.ts
Normal file
3
pages/api/bigcommerce/customers/login.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import loginApi from '@lib/bigcommerce/api/customers/login'
|
||||||
|
|
||||||
|
export default loginApi()
|
Loading…
x
Reference in New Issue
Block a user