mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 12:11:22 +00:00
Initial Commit
This commit is contained in:
parent
9e92abdda0
commit
4cdce5ed39
@ -40,7 +40,7 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
</Link>
|
</Link>
|
||||||
</li>
|
</li>
|
||||||
)}
|
)}
|
||||||
{process.env.COMMERCE_CUSTOMERAUTH_ENABLED && (
|
|
||||||
<li className={s.item}>
|
<li className={s.item}>
|
||||||
{customer ? (
|
{customer ? (
|
||||||
<DropdownMenu />
|
<DropdownMenu />
|
||||||
@ -54,7 +54,7 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</li>
|
</li>
|
||||||
)}
|
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
)
|
)
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
49
framework/kibocommerce/api/endpoints/login/login.ts
Normal file
49
framework/kibocommerce/api/endpoints/login/login.ts
Normal 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
|
@ -1,16 +1,42 @@
|
|||||||
import { MutationHook } from '@commerce/utils/types'
|
import { MutationHook } from '@commerce/utils/types'
|
||||||
import useLogin, { UseLogin } from '@commerce/auth/use-login'
|
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 default useLogin as UseLogin<typeof handler>
|
||||||
|
|
||||||
export const handler: MutationHook<any> = {
|
export const handler: MutationHook<LoginHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: '',
|
url: '/api/login',
|
||||||
|
method: 'POST',
|
||||||
|
query: ''
|
||||||
},
|
},
|
||||||
async fetcher() {
|
async fetcher({ input: { email, password }, options, fetch }) {
|
||||||
return null
|
if (!(email && password)) {
|
||||||
|
throw new CommerceError({
|
||||||
|
message:
|
||||||
|
'An 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]
|
||||||
|
)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,6 @@
|
|||||||
"wishlist": false,
|
"wishlist": false,
|
||||||
"cart": false,
|
"cart": false,
|
||||||
"search": false,
|
"search": false,
|
||||||
"customerAuth": false
|
"customerAuth": true
|
||||||
}
|
}
|
||||||
}
|
}
|
5
framework/kibocommerce/schema.d.ts
vendored
5
framework/kibocommerce/schema.d.ts
vendored
@ -11397,3 +11397,8 @@ export type WorkflowStateInput = {
|
|||||||
shipmentState?: Maybe<Scalars['String']>
|
shipmentState?: Maybe<Scalars['String']>
|
||||||
taskList?: Maybe<Array<Maybe<FulfillmentTaskInput>>>
|
taskList?: Maybe<Array<Maybe<FulfillmentTaskInput>>>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type LoginMutationVariables = Exact<{
|
||||||
|
email: Scalars['String']
|
||||||
|
password: Scalars['String']
|
||||||
|
}>
|
8
framework/kibocommerce/types/login.ts
Normal file
8
framework/kibocommerce/types/login.ts
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user