mirror of
https://github.com/vercel/commerce.git
synced 2025-07-04 04:01:21 +00:00
Final Changes
This commit is contained in:
parent
56dbee6955
commit
c71b2fb14d
@ -12,10 +12,9 @@ export type LoginEndpoint = LoginAPI['endpoint']
|
||||
|
||||
export const handlers: LoginEndpoint['handlers'] = { login }
|
||||
|
||||
const loginApi = createEndpoint<LoginAPI>({
|
||||
export const loginApi = createEndpoint<LoginAPI>({
|
||||
handler: loginEndpoint,
|
||||
handlers,
|
||||
})
|
||||
|
||||
export default loginApi
|
||||
|
||||
|
@ -1,16 +1,21 @@
|
||||
import { FetcherError } from '@commerce/utils/errors'
|
||||
import type { LoginEndpoint } from '.'
|
||||
import { loginMutation } from '../../../utils/mutations/login-mutation'
|
||||
import {prepareSetCookie} from '../../utils/prepareSetCookie';
|
||||
import {setCookies} from '../../utils/setCookie'
|
||||
|
||||
const invalidCredentials = /invalid credentials/i
|
||||
|
||||
let response;
|
||||
|
||||
const login: LoginEndpoint['handlers']['login'] = async ({
|
||||
req,
|
||||
res,
|
||||
body: { email, password },
|
||||
config,
|
||||
commerce,
|
||||
}) => {
|
||||
console.log('login hit', email, password)
|
||||
// TODO: Add proper validations with something like Ajv
|
||||
|
||||
if (!(email && password)) {
|
||||
return res.status(400).json({
|
||||
data: null,
|
||||
@ -18,12 +23,17 @@ const login: LoginEndpoint['handlers']['login'] = async ({
|
||||
})
|
||||
}
|
||||
try {
|
||||
/*
|
||||
const loginMutation = ` mutation loginAccount($input) { login($input) { } } `
|
||||
const variables = { input: { email, password } }
|
||||
const loginResponse = await config.fetch(loginMutation, { variables })
|
||||
setCookie(res)
|
||||
*/
|
||||
|
||||
response = await config.fetch(loginMutation, { variables: { loginInput : { username: email, password }}})
|
||||
const { account } = response.data;
|
||||
|
||||
const authCookie = prepareSetCookie(
|
||||
config.customerCookie,
|
||||
JSON.stringify(response.data.account),
|
||||
account.accessTokenExpiration ? { expires: new Date(account.accessTokenExpiration) }: {},
|
||||
)
|
||||
setCookies(res, [authCookie])
|
||||
|
||||
} catch (error) {
|
||||
// Check if the email and password didn't match an existing account
|
||||
if (
|
||||
@ -45,7 +55,7 @@ setCookie(res)
|
||||
throw error
|
||||
}
|
||||
|
||||
res.status(200).json({ data: null })
|
||||
res.status(200).json({ data: response })
|
||||
}
|
||||
|
||||
export default login
|
13
framework/kibocommerce/api/utils/prepareSetCookie.ts
Normal file
13
framework/kibocommerce/api/utils/prepareSetCookie.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function prepareSetCookie(name: string, value: string, options: any = {}): string {
|
||||
const cookieValue = [`${name}=${value}`];
|
||||
|
||||
if (options.maxAge) {
|
||||
cookieValue.push(`Max-Age=${options.maxAge}`);
|
||||
}
|
||||
|
||||
if (options.expires && !options.maxAge) {
|
||||
cookieValue.push(`Expires=${options.expires.toUTCString()}`);
|
||||
}
|
||||
|
||||
return cookieValue.join('; ');
|
||||
}
|
3
framework/kibocommerce/api/utils/setCookie.ts
Normal file
3
framework/kibocommerce/api/utils/setCookie.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function setCookies(res: any, cookies: string[]): void {
|
||||
res.setHeader('Set-Cookie', cookies);
|
||||
}
|
@ -5,14 +5,12 @@ import { useCallback } from 'react'
|
||||
import { CommerceError } from '@commerce/utils/errors'
|
||||
import type { LoginHook } from '../types/login'
|
||||
import useCustomer from '../customer/use-customer'
|
||||
import { loginMutation } from '../utils/mutations/login-mutation'
|
||||
export default useLogin as UseLogin<typeof handler>
|
||||
|
||||
export const handler: MutationHook<LoginHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/login',
|
||||
method: 'POST',
|
||||
query: loginMutation
|
||||
method: 'POST'
|
||||
},
|
||||
async fetcher({ input: { email, password }, options, fetch }) {
|
||||
if (!(email && password)) {
|
||||
|
@ -6,6 +6,7 @@ mutation login($loginInput:CustomerUserAuthInfoInput!) {
|
||||
userId
|
||||
refreshToken
|
||||
refreshTokenExpiration
|
||||
accessTokenExpiration
|
||||
customerAccount {
|
||||
id
|
||||
firstName
|
||||
|
Loading…
x
Reference in New Issue
Block a user