From 93436fbc0983f5d7b3c19fa1613380d191abc758 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 17 Jun 2023 18:27:51 +0200 Subject: [PATCH] Refactor rest password route --- .../src/routes/auth/ResetPasswordRoute.tsx | 175 ++++++++---------- 1 file changed, 73 insertions(+), 102 deletions(-) diff --git a/geneit_app/src/routes/auth/ResetPasswordRoute.tsx b/geneit_app/src/routes/auth/ResetPasswordRoute.tsx index c745db0..64d3299 100644 --- a/geneit_app/src/routes/auth/ResetPasswordRoute.tsx +++ b/geneit_app/src/routes/auth/ResetPasswordRoute.tsx @@ -6,67 +6,26 @@ import { TextField, Typography, } from "@mui/material"; -import React, { useEffect, useRef, useState } from "react"; +import React from "react"; import { useLocation } from "react-router-dom"; import { AuthApi, CheckResetTokenResponse } from "../../api/AuthApi"; -import { PasswordInput } from "../../widgets/PasswordInput"; -import { AuthSingleMessage } from "../../widgets/AuthSingleMessage"; import { ServerApi } from "../../api/ServerApi"; +import { AsyncWidget } from "../../widgets/AsyncWidget"; +import { AuthSingleMessage } from "../../widgets/AuthSingleMessage"; +import { PasswordInput } from "../../widgets/PasswordInput"; export function ResetPasswordRoute(): React.ReactElement { const [error, setError] = React.useState(null); + const [loading, setLoading] = React.useState(false); + const [success, setSuccess] = React.useState(false); + + const [password, setPassword] = React.useState(""); + const [confirmPassword, setConfirmPassword] = React.useState(""); const { hash } = useLocation(); const token = hash.substring(1); const [info, setInfo] = React.useState(null); - const checkedToken = useRef(null); - - useEffect(() => { - (async () => { - if (token === checkedToken.current) return; - checkedToken.current = token; - - try { - setError(null); - const info = await AuthApi.CheckResetPasswordToken(token); - setInfo(info); - } catch (e) { - console.error(e); - setError( - "Echec de validation du jeton de réinitialisation de mot de passe!" - ); - } - })(); - }); - - if (error) - return ( - - {error} - - ); - - if (info === null) - return ( - <> - - - ); - - return ; -} - -function ResetPasswordForm(p: { - token: string; - info: CheckResetTokenResponse; -}): React.ReactElement { - const [error, setError] = React.useState(null); - const [loading, setLoading] = React.useState(false); - const [success, setSuccess] = React.useState(false); - - const [password, setPassword] = useState(""); - const [confirmPassword, setConfirmPassword] = useState(""); const canSubmit = ServerApi.CheckPassword(password) === null && password === confirmPassword; @@ -79,7 +38,7 @@ function ResetPasswordForm(p: { setLoading(true); try { - await AuthApi.ResetPassword(p.token, password); + await AuthApi.ResetPassword(token, password); setSuccess(true); } catch (e) { console.error(e); @@ -102,57 +61,69 @@ function ResetPasswordForm(p: { ); return ( - <> - {error && ( - - {error} - + setInfo(await AuthApi.CheckResetPasswordToken(token))} + errMsg="Echec de validation du jeton de réinitialisation de mot de passe !" + build={() => ( + <> + {error && ( + + {error} + + )} + + + Bonjour {info!.name}, veuillez définir votre nouveau mot de passe : + + + { + setPassword(n); + }} + /> + + setConfirmPassword(e.target.value)} + label="Confirmer le mot de passe" + type="password" + id="password" + autoComplete="current-password" + /> + + + + )} - - - Bonjour {p.info.name}, veuillez définir votre nouveau mot de passe : - - - { - setPassword(n); - }} - /> - - setConfirmPassword(e.target.value)} - label="Confirmer le mot de passe" - type="password" - id="password" - autoComplete="current-password" - /> - - - - + /> ); }