mirror of
https://github.com/vercel/commerce.git
synced 2025-06-18 13:11:23 +00:00
77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { HookFetcher } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useCommerceLogin from '@commerce/use-login'
|
|
import useCustomer from '../customer/use-customer'
|
|
import {
|
|
ErrorResult,
|
|
LoginMutation,
|
|
LoginMutationVariables,
|
|
} from '@framework/schema'
|
|
|
|
export const loginMutation = /* GraphQL */ `
|
|
mutation login($username: String!, $password: String!) {
|
|
login(username: $username, password: $password) {
|
|
__typename
|
|
... on CurrentUser {
|
|
id
|
|
}
|
|
... on ErrorResult {
|
|
errorCode
|
|
message
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
export const fetcher: HookFetcher<LoginMutation, LoginMutationVariables> = (
|
|
options,
|
|
{ username, password },
|
|
fetch
|
|
) => {
|
|
if (!(username && password)) {
|
|
throw new CommerceError({
|
|
message: 'An email address and password are required to login',
|
|
})
|
|
}
|
|
|
|
return fetch({
|
|
...options,
|
|
query: loginMutation,
|
|
variables: { username, password },
|
|
})
|
|
}
|
|
|
|
export function extendHook(customFetcher: typeof fetcher) {
|
|
const useLogin = () => {
|
|
const { revalidate } = useCustomer()
|
|
const fn = useCommerceLogin<LoginMutation, LoginMutationVariables>(
|
|
{},
|
|
customFetcher
|
|
)
|
|
|
|
return useCallback(
|
|
async function login(input: { email: string; password: string }) {
|
|
const data = await fn({
|
|
username: input.email,
|
|
password: input.password,
|
|
})
|
|
if (data.login.__typename !== 'CurrentUser') {
|
|
throw new CommerceError({
|
|
message: (data.login as ErrorResult).message,
|
|
})
|
|
}
|
|
await revalidate()
|
|
return data
|
|
},
|
|
[fn]
|
|
)
|
|
}
|
|
|
|
useLogin.extend = extendHook
|
|
|
|
return useLogin
|
|
}
|
|
|
|
export default extendHook(fetcher)
|