import { useCallback } from 'react' import { MutationHook } from '@commerce/utils/types' import useLogin, { UseLogin } from '@commerce/auth/use-login' import { CommerceError, ValidationError } from '@commerce/utils/errors' import useCustomer from '../customer/use-customer' import { LoginMutation, LoginMutationVariables } from '../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 default useLogin as UseLogin export const handler: MutationHook = { 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({ ...options, variables, }) if (login.__typename !== 'CurrentUser') { throw new ValidationError(login) } return null }, useHook: ({ fetch }) => () => { const { revalidate } = useCustomer() return useCallback( async function login(input) { const data = await fetch({ input }) await revalidate() return data }, [fetch, revalidate] ) }, }