From 4268c9ab28fb39af5621de473f10f99931c832c1 Mon Sep 17 00:00:00 2001 From: GunaTrika Date: Mon, 19 Jul 2021 18:10:29 +0530 Subject: [PATCH] Enabling login - enabled login service - ep api is returing error but service call working fine --- framework/elasticpath/api/operations/login.ts | 2 +- framework/elasticpath/auth/use-login.tsx | 38 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) 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] + ) }, }