mirror of
https://github.com/vercel/commerce.git
synced 2025-03-29 00:36:04 +00:00
* Packages Bump * Code Updated * More API Changes * Working updates * Updated Tailwind Config * SWR API updates * More changes * Commercejs Types * Commercejs Types * Commercejs Types
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { MutationHook } from '@commerce/utils/types'
|
|
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
|
import { LoginHook } from '../types/login'
|
|
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
|
import useCustomer from '../customer/use-customer'
|
|
import { LoginMutation, LoginMutationVariables } from '../schema'
|
|
import { loginMutation } from '../utils/mutations/log-in-mutation'
|
|
|
|
export default useLogin as UseLogin<typeof handler>
|
|
|
|
export const handler: MutationHook<LoginHook> = {
|
|
fetchOptions: {
|
|
query: loginMutation,
|
|
},
|
|
async fetcher({ input: { email, password }, options, fetch }) {
|
|
if (!(email && password)) {
|
|
throw new CommerceError({
|
|
message: 'A email and password are required to login',
|
|
})
|
|
}
|
|
|
|
const variables: LoginMutationVariables = {
|
|
username: email,
|
|
password,
|
|
}
|
|
|
|
const { login } = await fetch<LoginMutation>({
|
|
...options,
|
|
variables,
|
|
})
|
|
|
|
if (login.__typename !== 'CurrentUser') {
|
|
throw new ValidationError(login)
|
|
}
|
|
|
|
return null
|
|
},
|
|
useHook:
|
|
({ fetch }) =>
|
|
() => {
|
|
const { mutate } = useCustomer()
|
|
|
|
return useCallback(
|
|
async function login(input) {
|
|
const data = await fetch({ input })
|
|
await mutate()
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|