mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
- Extended motlin sdk and corrected pcm ur - Using new client for below sections - Get all products - Get product - Get all products path - Login & Signup - Customers - Changing product normalizer for pcm products
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
|
import type { LoginHook } from '../types/login'
|
|
import useCustomer from '../customer/use-customer'
|
|
import epClient from '../utils/ep-client'
|
|
|
|
export default useLogin as UseLogin<typeof handler>
|
|
|
|
export const handler: MutationHook<any> = {
|
|
fetchOptions: {
|
|
query: ''
|
|
},
|
|
async fetcher({ input: { email, password }, options, fetch }) {
|
|
if (!(email && password)) {
|
|
throw new CommerceError({
|
|
message:
|
|
'A first name, last name, email and password are required to login',
|
|
})
|
|
}
|
|
|
|
let token = await epClient.Customers.TokenViaPassword(email, password);
|
|
return token || null;
|
|
},
|
|
useHook: ({ fetch }) => () => {
|
|
const { revalidate } = useCustomer()
|
|
|
|
return useCallback(
|
|
async function login(input) {
|
|
const data = await fetch({ input })
|
|
await revalidate()
|
|
return data
|
|
},
|
|
[fetch, revalidate]
|
|
)
|
|
},
|
|
}
|