From 386ba6c32eacab53663f0a0eafe95df1cfa5d896 Mon Sep 17 00:00:00 2001 From: Maxime Lalo Date: Mon, 27 Nov 2023 10:02:43 +0100 Subject: [PATCH] :sparkles: Two first steps working --- src/front/Api/Auth/Customer/Auth.ts | 19 ++++++ .../Layouts/LoginCustomer/StepEmail/index.tsx | 42 ++----------- .../Layouts/LoginCustomer/StepTotp/index.tsx | 47 +++----------- .../Layouts/LoginCustomer/index.tsx | 63 ++++++++++++++++++- 4 files changed, 92 insertions(+), 79 deletions(-) diff --git a/src/front/Api/Auth/Customer/Auth.ts b/src/front/Api/Auth/Customer/Auth.ts index 7d0481f2..d629f0eb 100644 --- a/src/front/Api/Auth/Customer/Auth.ts +++ b/src/front/Api/Auth/Customer/Auth.ts @@ -8,6 +8,15 @@ export type IMailVerifyReturn = { partialPhoneNumber: string; }; +export type IVerifyTotpCodeParams = { + totpCode: string; + email: string; +}; + +export type IVerifyTotpCodeReturn = { + validCode: boolean; +}; + export default class Auth extends BaseApiService { private static instance: Auth; protected readonly namespaceUrl = this.getBaseUrl().concat("/customer"); @@ -26,4 +35,14 @@ export default class Auth extends BaseApiService { return Promise.reject(err); } } + + public async verifyTotpCode(body: IVerifyTotpCodeParams): Promise { + const url = new URL(this.baseURl.concat("/verify-totp-code")); + try { + return this.postRequest(url, body); + } catch (err) { + this.onError(err); + return Promise.reject(err); + } + } } diff --git a/src/front/Components/Layouts/LoginCustomer/StepEmail/index.tsx b/src/front/Components/Layouts/LoginCustomer/StepEmail/index.tsx index 63607abe..5a2595f6 100644 --- a/src/front/Components/Layouts/LoginCustomer/StepEmail/index.tsx +++ b/src/front/Components/Layouts/LoginCustomer/StepEmail/index.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useState } from "react"; +import React, { useCallback } from "react"; import classes from "./classes.module.scss"; import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography"; import Image from "next/image"; @@ -9,16 +9,13 @@ import franceConnectLogo from "../france-connect.svg"; import { useRouter } from "next/router"; import Customers from "@Front/Api/Auth/Id360/Customers/Customers"; import { ValidationError } from "class-validator"; -import { LoginStep } from ".."; -import Auth from "@Front/Api/Auth/Customer/Auth"; type IProps = { - setPartialPhoneNumber: (phoneNumber: string) => void; - setStep: (step: LoginStep) => void; + onSubmit: (e: React.FormEvent | null, values: { [key: string]: string }) => void; + validationErrors: ValidationError[]; }; export default function StepEmail(props: IProps) { - const { setPartialPhoneNumber, setStep } = props; - const [validationError, setValidationError] = useState([]); + const { onSubmit, validationErrors } = props; const router = useRouter(); const redirectCustomerOnConnection = useCallback(() => { async function getCustomer() { @@ -32,33 +29,6 @@ export default function StepEmail(props: IProps) { getCustomer(); }, [router]); - const onSubmitHandler = useCallback( - async (e: React.FormEvent | null, values: { [key: string]: string }) => { - try { - if (!values["email"]) return; - const res = await Auth.getInstance().mailVerifySms({ email: values["email"] }); - setPartialPhoneNumber(res.partialPhoneNumber); - setStep(LoginStep.TOTP); - } catch (error: any) { - // If token already exists and is still valid redirect to the connect/register page - if (error.http_status === 425) { - setStep(LoginStep.TOTP); - return; - } - setValidationError([ - { - property: "email", - constraints: { - [error.http_status]: error.message, - }, - }, - ]); - return; - } - }, - [setPartialPhoneNumber, setStep], - ); - return (
@@ -70,11 +40,11 @@ export default function StepEmail(props: IProps) { Ou -
+ error.property === "email")} + validationError={validationErrors.find((error) => error.property === "email")} />