diff --git a/framework/kibocommerce/api/endpoints/login/index.ts b/framework/kibocommerce/api/endpoints/login/index.ts index ed3200058..49476c7f6 100644 --- a/framework/kibocommerce/api/endpoints/login/index.ts +++ b/framework/kibocommerce/api/endpoints/login/index.ts @@ -12,10 +12,9 @@ export type LoginEndpoint = LoginAPI['endpoint'] export const handlers: LoginEndpoint['handlers'] = { login } -const loginApi = createEndpoint({ +export 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 index d90a93457..699fa0fbf 100644 --- a/framework/kibocommerce/api/endpoints/login/login.ts +++ b/framework/kibocommerce/api/endpoints/login/login.ts @@ -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 \ No newline at end of file diff --git a/framework/kibocommerce/api/utils/prepareSetCookie.ts b/framework/kibocommerce/api/utils/prepareSetCookie.ts new file mode 100644 index 000000000..3d9b3380a --- /dev/null +++ b/framework/kibocommerce/api/utils/prepareSetCookie.ts @@ -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('; '); +} \ No newline at end of file diff --git a/framework/kibocommerce/api/utils/setCookie.ts b/framework/kibocommerce/api/utils/setCookie.ts new file mode 100644 index 000000000..2c194c921 --- /dev/null +++ b/framework/kibocommerce/api/utils/setCookie.ts @@ -0,0 +1,3 @@ +export function setCookies(res: any, cookies: string[]): void { + res.setHeader('Set-Cookie', cookies); +} \ No newline at end of file diff --git a/framework/kibocommerce/auth/use-login.tsx b/framework/kibocommerce/auth/use-login.tsx index eec15965d..c0197e4c2 100644 --- a/framework/kibocommerce/auth/use-login.tsx +++ b/framework/kibocommerce/auth/use-login.tsx @@ -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 export const handler: MutationHook = { fetchOptions: { url: '/api/login', - method: 'POST', - query: loginMutation + method: 'POST' }, async fetcher({ input: { email, password }, options, fetch }) { if (!(email && password)) { diff --git a/framework/kibocommerce/utils/mutations/login-mutation.ts b/framework/kibocommerce/utils/mutations/login-mutation.ts index b4b655cc3..61a9d03a5 100644 --- a/framework/kibocommerce/utils/mutations/login-mutation.ts +++ b/framework/kibocommerce/utils/mutations/login-mutation.ts @@ -6,6 +6,7 @@ mutation login($loginInput:CustomerUserAuthInfoInput!) { userId refreshToken refreshTokenExpiration + accessTokenExpiration customerAccount { id firstName