forked from crowetic/commerce
41 lines
1.0 KiB
TypeScript
41 lines
1.0 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'
|
|
|
|
export default useLogin as UseLogin<typeof handler>
|
|
|
|
export const handler: MutationHook<LoginHook> = {
|
|
fetchOptions: {
|
|
url: '/api/login',
|
|
method: 'POST',
|
|
},
|
|
async fetcher({ input: { email, password }, options, fetch }) {
|
|
if (!(email && password)) {
|
|
throw new CommerceError({
|
|
message:
|
|
'An email and password are required to login',
|
|
})
|
|
}
|
|
|
|
return fetch({
|
|
...options,
|
|
body: { email, password },
|
|
})
|
|
},
|
|
useHook: ({ fetch }) => () => {
|
|
const { revalidate } = useCustomer()
|
|
|
|
return useCallback(
|
|
async function login(input) {
|
|
const data = await fetch({ input })
|
|
await revalidate()
|
|
return data
|
|
},
|
|
[fetch, revalidate]
|
|
)
|
|
},
|
|
}
|