Initial Commit

This commit is contained in:
SushantJadhav 2021-08-25 10:28:44 +05:30
parent 9e92abdda0
commit 4cdce5ed39
7 changed files with 119 additions and 11 deletions

View File

@ -40,7 +40,7 @@ const UserNav: FC<Props> = ({ className }) => {
</Link>
</li>
)}
{process.env.COMMERCE_CUSTOMERAUTH_ENABLED && (
<li className={s.item}>
{customer ? (
<DropdownMenu />
@ -54,7 +54,7 @@ const UserNav: FC<Props> = ({ className }) => {
</button>
)}
</li>
)}
</ul>
</nav>
)

View File

@ -1 +1,21 @@
export default function noopApi(...args: any[]): void {}
// export default function noopApi(...args: any[]): void {}
import { GetAPISchema, createEndpoint } from '@commerce/api'
import loginEndpoint from '@commerce/api/endpoints/login'
import type { LoginSchema } from '../../../types/login'
import type { KiboCommerceAPI } from '../..'
import login from './login'
export type LoginAPI = GetAPISchema<KiboCommerceAPI, LoginSchema>
export type LoginEndpoint = LoginAPI['endpoint']
export const handlers: LoginEndpoint['handlers'] = { login }
const loginApi = createEndpoint<LoginAPI>({
handler: loginEndpoint,
handlers,
})
export default loginApi

View File

@ -0,0 +1,49 @@
import { FetcherError } from '@commerce/utils/errors'
import type { LoginEndpoint } from '.'
const invalidCredentials = /invalid credentials/i
const login: LoginEndpoint['handlers']['login'] = async ({
res,
body: { email, password },
config,
commerce,
}) => {
// TODO: Add proper validations with something like Ajv
if (!(email && password)) {
return res.status(400).json({
data: null,
errors: [{ message: 'Invalid request' }],
})
}
// TODO: validate the password and email
// Passwords must be at least 7 characters and contain both alphabetic
// and numeric characters.
try {
await commerce.login({ variables: { email, password }, config, res })
} catch (error) {
// Check if the email and password didn't match an existing account
if (
error instanceof FetcherError &&
invalidCredentials.test(error.message)
) {
return res.status(401).json({
data: null,
errors: [
{
message:
'Cannot find an account that matches the provided credentials',
code: 'invalid_credentials',
},
],
})
}
throw error
}
res.status(200).json({ data: null })
}
export default login

View File

@ -1,16 +1,42 @@
import { MutationHook } from '@commerce/utils/types'
import useLogin, { UseLogin } from '@commerce/auth/use-login'
import { useCallback } from 'react'
import { CommerceError } from '@commerce/utils/errors'
import type { LoginHook } from '../types/login'
import useCustomer from '../customer/use-customer'
export default useLogin as UseLogin<typeof handler>
export const handler: MutationHook<any> = {
export const handler: MutationHook<LoginHook> = {
fetchOptions: {
query: '',
url: '/api/login',
method: 'POST',
query: ''
},
async fetcher() {
return null
async fetcher({ input: { email, password }, options, fetch }) {
if (!(email && password)) {
throw new CommerceError({
message:
'An 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]
)
},
}

View File

@ -4,6 +4,6 @@
"wishlist": false,
"cart": false,
"search": false,
"customerAuth": false
"customerAuth": true
}
}

View File

@ -11397,3 +11397,8 @@ export type WorkflowStateInput = {
shipmentState?: Maybe<Scalars['String']>
taskList?: Maybe<Array<Maybe<FulfillmentTaskInput>>>
}
export type LoginMutationVariables = Exact<{
email: Scalars['String']
password: Scalars['String']
}>

View File

@ -0,0 +1,8 @@
import * as Core from '@commerce/types/login'
import type { LoginMutationVariables } from '../schema'
export * from '@commerce/types/login'
export type LoginOperation = Core.LoginOperation & {
variables: LoginMutationVariables
}