diff --git a/framework/elasticpath/api/operations/login.ts b/framework/elasticpath/api/operations/login.ts index 76cbb177d..a1ff517e0 100644 --- a/framework/elasticpath/api/operations/login.ts +++ b/framework/elasticpath/api/operations/login.ts @@ -8,7 +8,7 @@ import { Provider, ElasticpathConfig } from '..' export default function loginOperation({ commerce, -}: OperationContext) { +}: OperationContext) { async function login(opts: { variables: T['variables'] config?: Partial diff --git a/framework/elasticpath/auth/use-login.tsx b/framework/elasticpath/auth/use-login.tsx index 28351dc7f..3ebacc9b7 100644 --- a/framework/elasticpath/auth/use-login.tsx +++ b/framework/elasticpath/auth/use-login.tsx @@ -1,16 +1,40 @@ -import { MutationHook } from '@commerce/utils/types' +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 -export const handler: MutationHook = { +export const handler: MutationHook = { fetchOptions: { - query: '', + url: '/api/login', + method: 'POST', }, - async fetcher() { - return null + 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', + }) + } + + return fetch({ + ...options, + body: { email, password }, + }) }, - useHook: () => () => { - return async function () {} + useHook: ({ fetch }) => () => { + const { revalidate } = useCustomer() + + return useCallback( + async function login(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) }, }