Enabling login

- enabled login service
 - ep api is returing error but service call working fine
This commit is contained in:
GunaTrika 2021-07-19 18:10:29 +05:30
parent 62e03f518c
commit 4268c9ab28
2 changed files with 32 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import { Provider, ElasticpathConfig } from '..'
export default function loginOperation({ export default function loginOperation({
commerce, commerce,
}: OperationContext<Provider>) { }: OperationContext<Provider | any>) {
async function login<T extends LoginOperation>(opts: { async function login<T extends LoginOperation>(opts: {
variables: T['variables'] variables: T['variables']
config?: Partial<ElasticpathConfig> config?: Partial<ElasticpathConfig>

View File

@ -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 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 default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<any> = { export const handler: MutationHook<LoginHook> = {
fetchOptions: { fetchOptions: {
query: '', url: '/api/login',
method: 'POST',
}, },
async fetcher() { async fetcher({ input: { email, password }, options, fetch }) {
return null 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: () => () => { useHook: ({ fetch }) => () => {
return async function () {} const { revalidate } = useCustomer()
return useCallback(
async function login(input) {
const data = await fetch({ input })
await revalidate()
return data
},
[fetch, revalidate]
)
}, },
} }