From fea205943330d4e760d4bfa776c3ad5869d4095e Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Fri, 9 Jun 2023 18:27:03 +0200 Subject: [PATCH] Can sign out --- geneit_app/src/api/AuthApi.ts | 12 +++++++ geneit_app/src/routes/auth/OIDCCbRoute.tsx | 4 ++- .../src/widgets/BaseAuthenticatedPage.tsx | 32 +++++++++++++------ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/geneit_app/src/api/AuthApi.ts b/geneit_app/src/api/AuthApi.ts index a99db50..08266d0 100644 --- a/geneit_app/src/api/AuthApi.ts +++ b/geneit_app/src/api/AuthApi.ts @@ -50,4 +50,16 @@ export class AuthApi { sessionStorage.setItem(TokenStateKey, res.token); } + + /** + * Sign out + */ + static async SignOut(): Promise { + await APIClient.exec({ + uri: "/auth/logout", + method: "GET", + }); + + sessionStorage.removeItem(TokenStateKey); + } } diff --git a/geneit_app/src/routes/auth/OIDCCbRoute.tsx b/geneit_app/src/routes/auth/OIDCCbRoute.tsx index f7c6a27..7313a52 100644 --- a/geneit_app/src/routes/auth/OIDCCbRoute.tsx +++ b/geneit_app/src/routes/auth/OIDCCbRoute.tsx @@ -1,6 +1,6 @@ import { Button, CircularProgress } from "@mui/material"; import { useEffect, useRef, useState } from "react"; -import { Link, useSearchParams } from "react-router-dom"; +import { Link, useNavigate, useSearchParams } from "react-router-dom"; import { AuthApi } from "../../api/AuthApi"; import { useSetAtom } from "jotai"; @@ -9,6 +9,7 @@ import { useSetAtom } from "jotai"; */ export function OIDCCbRoute(): React.ReactElement { const setAuth = useSetAtom(AuthApi.authStatus); + const navigate = useNavigate(); const [error, setError] = useState(false); @@ -27,6 +28,7 @@ export function OIDCCbRoute(): React.ReactElement { count.current = code!; await AuthApi.FinishOpenIDLogin(code!, state!); + navigate("/"); setAuth(true); } catch (e) { console.error(e); diff --git a/geneit_app/src/widgets/BaseAuthenticatedPage.tsx b/geneit_app/src/widgets/BaseAuthenticatedPage.tsx index da662b3..04895b4 100644 --- a/geneit_app/src/widgets/BaseAuthenticatedPage.tsx +++ b/geneit_app/src/widgets/BaseAuthenticatedPage.tsx @@ -1,30 +1,42 @@ -import AccountCircle from "@mui/icons-material/AccountCircle"; -import { Alert, Button, CircularProgress, Grid } from "@mui/material"; +import { mdiFamilyTree } from "@mdi/js"; +import Icon from "@mdi/react"; +import SettingsIcon from "@mui/icons-material/Settings"; +import { Alert, Button, CircularProgress } from "@mui/material"; import AppBar from "@mui/material/AppBar"; -import IconButton from "@mui/material/IconButton"; import Menu from "@mui/material/Menu"; import MenuItem from "@mui/material/MenuItem"; import Toolbar from "@mui/material/Toolbar"; import Typography from "@mui/material/Typography"; +import { useSetAtom } from "jotai"; import * as React from "react"; +import { Outlet, useNavigate } from "react-router-dom"; +import { AuthApi } from "../api/AuthApi"; import { User, UserApi } from "../api/UserApi"; -import { Outlet } from "react-router-dom"; -import SettingsIcon from "@mui/icons-material/Settings"; export function BaseAuthenticatedPage(): React.ReactElement { const [user, setUser] = React.useState(null); const [error, setError] = React.useState(false); + const setSignedIn = useSetAtom(AuthApi.authStatus); + const navigate = useNavigate(); + const [anchorEl, setAnchorEl] = React.useState(null); const handleMenu = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; - const handleClose = () => { + const handleCloseMenu = () => { setAnchorEl(null); }; + const signOut = () => { + handleCloseMenu(); + AuthApi.SignOut(); + navigate("/"); + setSignedIn(false); + }; + React.useEffect(() => { const load = async () => { if (error || user != null) return; @@ -80,10 +92,10 @@ export function BaseAuthenticatedPage(): React.ReactElement { <> + GeneIT -