From 156597d3d1398022c204bca9a636ea73a30e727e Mon Sep 17 00:00:00 2001
From: Lucas Oliveira <62335616+lucaseduoli@users.noreply.github.com>
Date: Fri, 6 Dec 2024 12:01:13 -0300
Subject: [PATCH] fix: redirect to previous page after login (#5102)
* Added redirect handling to autologin and Protected Route
* Redirect after login on ProtectedLoginRoute
---
.../components/authorization/authGuard/index.tsx | 13 ++++++++++++-
.../authorization/authLoginGuard/index.tsx | 12 +++++++-----
.../API/queries/auth/use-get-autologin.ts | 7 ++++++-
3 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/src/frontend/src/components/authorization/authGuard/index.tsx b/src/frontend/src/components/authorization/authGuard/index.tsx
index 418d4561d..f01529301 100644
--- a/src/frontend/src/components/authorization/authGuard/index.tsx
+++ b/src/frontend/src/components/authorization/authGuard/index.tsx
@@ -31,7 +31,18 @@ export const ProtectedRoute = ({ children }) => {
}
}, [isAuthenticated]);
if (!isAuthenticated && autoLogin !== undefined && !autoLogin) {
- return ;
+ const currentPath = window.location.pathname;
+ const isHomePath = currentPath === "/" || currentPath === "/flows";
+ const isLoginPage = location.pathname.includes("login");
+ return (
+
+ );
} else {
return children;
}
diff --git a/src/frontend/src/components/authorization/authLoginGuard/index.tsx b/src/frontend/src/components/authorization/authLoginGuard/index.tsx
index 9c2c99beb..daf0628de 100644
--- a/src/frontend/src/components/authorization/authLoginGuard/index.tsx
+++ b/src/frontend/src/components/authorization/authLoginGuard/index.tsx
@@ -5,12 +5,14 @@ export const ProtectedLoginRoute = ({ children }) => {
const autoLogin = useAuthStore((state) => state.autoLogin);
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
- if (autoLogin === true) {
- return ;
- }
+ if (autoLogin === true || isAuthenticated) {
+ const urlParams = new URLSearchParams(window.location.search);
+ const redirectPath = urlParams.get("redirect");
- if (isAuthenticated) {
- return ;
+ if (redirectPath) {
+ return ;
+ }
+ return ;
}
return children;
diff --git a/src/frontend/src/controllers/API/queries/auth/use-get-autologin.ts b/src/frontend/src/controllers/API/queries/auth/use-get-autologin.ts
index c6bf6e63b..e5b9888a1 100644
--- a/src/frontend/src/controllers/API/queries/auth/use-get-autologin.ts
+++ b/src/frontend/src/controllers/API/queries/auth/use-get-autologin.ts
@@ -44,7 +44,12 @@ export const useGetAutoLogin: useQueryFunctionType = (
if (!isLoginPage) {
if (!isAuthenticated) {
await mutationLogout();
- navigate("/login");
+ const currentPath = window.location.pathname;
+ const isHomePath = currentPath === "/" || currentPath === "/flows";
+ navigate(
+ "/login" +
+ (!isHomePath && !isLoginPage ? "?redirect=" + currentPath : ""),
+ );
} else {
getUser();
}