From 75ecf9978b7cc2658205f1991d4665b68a355347 Mon Sep 17 00:00:00 2001 From: Sina Date: Thu, 11 Jul 2024 16:25:31 +0200 Subject: [PATCH] [fix] do not show login screen when already authenticated (#266) --- src/AppRouter.tsx | 4 +++- src/core/auth/RequireAuth.tsx | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/AppRouter.tsx b/src/AppRouter.tsx index 0f1d3f1c..a8bbd60d 100644 --- a/src/AppRouter.tsx +++ b/src/AppRouter.tsx @@ -31,7 +31,9 @@ export const AppRouter = memo( () => ( }> - } /> + }> + } /> + }> } /> } /> diff --git a/src/core/auth/RequireAuth.tsx b/src/core/auth/RequireAuth.tsx index 28d86e2a..f2e2b857 100644 --- a/src/core/auth/RequireAuth.tsx +++ b/src/core/auth/RequireAuth.tsx @@ -2,12 +2,18 @@ import { Outlet, useLocation } from 'react-router-dom' import { useAbsoluteNavigate } from 'src/shared/absolute-navigate' import { useUserProfile } from './useUserProfile' -export function RequireAuth() { +interface RequireAuthProps { + reverse?: boolean +} + +export function RequireAuth({ reverse }: RequireAuthProps) { const navigate = useAbsoluteNavigate() const location = useLocation() const user = useUserProfile() - if (!user.isAuthenticated && location.pathname !== '/auth/login') { + const isAuthenticated = !user.isAuthenticated && location.pathname !== '/auth/login' + + if (isAuthenticated && !reverse) { window.setTimeout(() => { navigate( { @@ -20,6 +26,11 @@ export function RequireAuth() { ) }) return null + } else if (reverse && !isAuthenticated) { + window.setTimeout(() => { + navigate(window.decodeURIComponent(location.search?.split('returnUrl=')[1]?.split('&')[0] ?? '/'), { replace: true }) + }) + return null } return