From 1bd18133b384971a0b8af5c143d4d1eb45f1b8e0 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Mon, 12 Jun 2023 16:25:38 +0200 Subject: [PATCH] Check reset token validity --- geneit_app/src/App.tsx | 2 + geneit_app/src/api/AuthApi.ts | 19 +++++++ .../src/routes/auth/ResetPasswordRoute.tsx | 54 +++++++++++++++++++ geneit_app/src/widgets/BaseLoginpage.tsx | 21 +++++--- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 geneit_app/src/routes/auth/ResetPasswordRoute.tsx diff --git a/geneit_app/src/App.tsx b/geneit_app/src/App.tsx index b83f5f0..dfff5ac 100644 --- a/geneit_app/src/App.tsx +++ b/geneit_app/src/App.tsx @@ -8,6 +8,7 @@ import { OIDCCbRoute } from "./routes/auth/OIDCCbRoute"; import { useAtom } from "jotai"; import { BaseAuthenticatedPage } from "./widgets/BaseAuthenticatedPage"; import { PasswordForgottenRoute } from "./routes/auth/PasswordForgottenRoute"; +import { ResetPasswordRoute } from "./routes/auth/ResetPasswordRoute"; /** * Core app @@ -27,6 +28,7 @@ function App() { path="password_forgotten" element={} /> + } /> } /> )} diff --git a/geneit_app/src/api/AuthApi.ts b/geneit_app/src/api/AuthApi.ts index 3ed99a9..40e64e0 100644 --- a/geneit_app/src/api/AuthApi.ts +++ b/geneit_app/src/api/AuthApi.ts @@ -1,6 +1,10 @@ import { atom } from "jotai"; import { APIClient } from "./ApiClient"; +export interface CheckResetTokenResponse { + name: string; +} + const TokenStateKey = "auth-token"; export class AuthApi { @@ -73,4 +77,19 @@ export class AuthApi { jsonData: { mail: mail }, }); } + + /** + * Check reset password token + */ + static async CheckResetPasswordToken( + token: string + ): Promise { + return ( + await APIClient.exec({ + uri: "/auth/check_reset_password_token", + method: "POST", + jsonData: { token: token }, + }) + ).data; + } } diff --git a/geneit_app/src/routes/auth/ResetPasswordRoute.tsx b/geneit_app/src/routes/auth/ResetPasswordRoute.tsx new file mode 100644 index 0000000..1d48918 --- /dev/null +++ b/geneit_app/src/routes/auth/ResetPasswordRoute.tsx @@ -0,0 +1,54 @@ +import { Alert, CircularProgress } from "@mui/material"; +import React, { useEffect, useRef } from "react"; +import { useLocation } from "react-router-dom"; +import { AuthApi, CheckResetTokenResponse } from "../../api/AuthApi"; + +export function ResetPasswordRoute(): React.ReactElement { + const [error, setError] = React.useState(null); + + const { hash } = useLocation(); + const token = hash.substring(1); + + const info = React.useState(null); + const checkedToken = useRef(null); + + useEffect(() => { + (async () => { + if (token === checkedToken.current) return; + checkedToken.current = token; + + try { + setError(null); + await AuthApi.CheckResetPasswordToken(token); + } 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 { + return

TODO

; +} diff --git a/geneit_app/src/widgets/BaseLoginpage.tsx b/geneit_app/src/widgets/BaseLoginpage.tsx index d5ed048..258a607 100644 --- a/geneit_app/src/widgets/BaseLoginpage.tsx +++ b/geneit_app/src/widgets/BaseLoginpage.tsx @@ -4,11 +4,10 @@ import Avatar from "@mui/material/Avatar"; import Box from "@mui/material/Box"; import CssBaseline from "@mui/material/CssBaseline"; import Grid from "@mui/material/Grid"; -import Link from "@mui/material/Link"; import Paper from "@mui/material/Paper"; import Typography from "@mui/material/Typography"; import * as React from "react"; -import { Outlet } from "react-router-dom"; +import { Link, Outlet } from "react-router-dom"; function Copyright(props: any) { return ( @@ -20,9 +19,15 @@ function Copyright(props: any) { {...props} > {"Copyright © "} - + Pierre HUBERT - {" "} + {" "} {new Date().getFullYear()} {"."} @@ -71,9 +76,11 @@ export function BaseLoginPage() { - - GeneIT - + + + GeneIT + +