From 4cdce5ed392689edb98fc9341036e7ebbca19de7 Mon Sep 17 00:00:00 2001 From: SushantJadhav Date: Wed, 25 Aug 2021 10:28:44 +0530 Subject: [PATCH] Initial Commit --- components/common/UserNav/UserNav.tsx | 4 +- .../kibocommerce/api/endpoints/login/index.ts | 22 ++++++++- .../kibocommerce/api/endpoints/login/login.ts | 49 +++++++++++++++++++ framework/kibocommerce/auth/use-login.tsx | 38 +++++++++++--- framework/kibocommerce/commerce.config.json | 4 +- framework/kibocommerce/schema.d.ts | 5 ++ framework/kibocommerce/types/login.ts | 8 +++ 7 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 framework/kibocommerce/api/endpoints/login/login.ts create mode 100644 framework/kibocommerce/types/login.ts diff --git a/components/common/UserNav/UserNav.tsx b/components/common/UserNav/UserNav.tsx index 5d7d28df3..07e1af04d 100644 --- a/components/common/UserNav/UserNav.tsx +++ b/components/common/UserNav/UserNav.tsx @@ -40,7 +40,7 @@ const UserNav: FC = ({ className }) => { )} - {process.env.COMMERCE_CUSTOMERAUTH_ENABLED && ( +
  • {customer ? ( @@ -54,7 +54,7 @@ const UserNav: FC = ({ className }) => { )}
  • - )} + ) diff --git a/framework/kibocommerce/api/endpoints/login/index.ts b/framework/kibocommerce/api/endpoints/login/index.ts index 491bf0ac9..ed3200058 100644 --- a/framework/kibocommerce/api/endpoints/login/index.ts +++ b/framework/kibocommerce/api/endpoints/login/index.ts @@ -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 + +export type LoginEndpoint = LoginAPI['endpoint'] + +export const handlers: LoginEndpoint['handlers'] = { login } + +const loginApi = createEndpoint({ + handler: loginEndpoint, + handlers, +}) + +export default loginApi + diff --git a/framework/kibocommerce/api/endpoints/login/login.ts b/framework/kibocommerce/api/endpoints/login/login.ts new file mode 100644 index 000000000..f55c3b54f --- /dev/null +++ b/framework/kibocommerce/api/endpoints/login/login.ts @@ -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 diff --git a/framework/kibocommerce/auth/use-login.tsx b/framework/kibocommerce/auth/use-login.tsx index 28351dc7f..60f8f4697 100644 --- a/framework/kibocommerce/auth/use-login.tsx +++ b/framework/kibocommerce/auth/use-login.tsx @@ -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 -export const handler: MutationHook = { +export const handler: MutationHook = { 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] + ) }, } diff --git a/framework/kibocommerce/commerce.config.json b/framework/kibocommerce/commerce.config.json index 0871b8849..8992569c6 100644 --- a/framework/kibocommerce/commerce.config.json +++ b/framework/kibocommerce/commerce.config.json @@ -4,6 +4,6 @@ "wishlist": false, "cart": false, "search": false, - "customerAuth": false + "customerAuth": true } -} +} \ No newline at end of file diff --git a/framework/kibocommerce/schema.d.ts b/framework/kibocommerce/schema.d.ts index cf52ddec9..430ce10b9 100644 --- a/framework/kibocommerce/schema.d.ts +++ b/framework/kibocommerce/schema.d.ts @@ -11397,3 +11397,8 @@ export type WorkflowStateInput = { shipmentState?: Maybe taskList?: Maybe>> } + +export type LoginMutationVariables = Exact<{ + email: Scalars['String'] + password: Scalars['String'] +}> \ No newline at end of file diff --git a/framework/kibocommerce/types/login.ts b/framework/kibocommerce/types/login.ts new file mode 100644 index 000000000..24d5077ff --- /dev/null +++ b/framework/kibocommerce/types/login.ts @@ -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 +}