From 3315bd53bdb6795ba7c0b7365a3eafb6f4d217d9 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 27 Nov 2023 10:54:07 +0100 Subject: [PATCH] :sparkles: Login working --- src/front/Api/Auth/Customer/Auth.ts | 16 ++++++++++++++++ .../Components/Layouts/LoginCustomer/index.tsx | 12 ++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/front/Api/Auth/Customer/Auth.ts b/src/front/Api/Auth/Customer/Auth.ts index 60c0788d..c04686c1 100644 --- a/src/front/Api/Auth/Customer/Auth.ts +++ b/src/front/Api/Auth/Customer/Auth.ts @@ -25,6 +25,12 @@ export type ISetPasswordParams = { totpCode: string; }; +export type ILoginParams = { + password: string; + email: string; + totpCode: string; +}; + export default class Auth extends BaseApiService { private static instance: Auth; protected readonly namespaceUrl = this.getBaseUrl().concat("/customer"); @@ -63,4 +69,14 @@ export default class Auth extends BaseApiService { return Promise.reject(err); } } + + public async login(body: ILoginParams): Promise { + const url = new URL(this.baseURl.concat("/login")); + try { + return this.postRequest(url, body); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } } diff --git a/src/front/Components/Layouts/LoginCustomer/index.tsx b/src/front/Components/Layouts/LoginCustomer/index.tsx index 4418fa4d..788c1700 100644 --- a/src/front/Components/Layouts/LoginCustomer/index.tsx +++ b/src/front/Components/Layouts/LoginCustomer/index.tsx @@ -49,9 +49,9 @@ export default function Login() { async (e: React.FormEvent | null, values: { [key: string]: string }) => { try { if (!values["email"]) return; + setEmail(values["email"]); const res = await Auth.getInstance().mailVerifySms({ email: values["email"] }); setPartialPhoneNumber(res.partialPhoneNumber); - setEmail(values["email"]); setStep(LoginStep.TOTP); } catch (error: any) { // If token already exists and is still valid redirect to the connect/register page @@ -133,16 +133,16 @@ export default function Login() { return; } }, - [email, totpCode, setValidationErrors], + [totpCode, email, router], ); const onPasswordSubmit = useCallback( async (e: React.FormEvent | null, values: { [key: string]: string }) => { try { if (!values["password"]) return; - const res = await Auth.getInstance().setPassword({ totpCode, email, password: values["password"] }); - - // If set password worked, setting the token and redirecting + const token = await Auth.getInstance().login({ totpCode, email, password: values["password"] }); + CustomerStore.instance.connect(token.accessToken, token.refreshToken); + router.push(Module.getInstance().get().modules.pages.Folder.pages.Select.props.path); } catch (error: any) { setValidationErrors([ { @@ -155,7 +155,7 @@ export default function Login() { return; } }, - [email, totpCode], + [email, router, totpCode], ); return (