fix: redirect to previous page after login (#5102)
* Added redirect handling to autologin and Protected Route * Redirect after login on ProtectedLoginRoute
This commit is contained in:
parent
b2691eebce
commit
156597d3d1
3 changed files with 25 additions and 7 deletions
|
|
@ -31,7 +31,18 @@ export const ProtectedRoute = ({ children }) => {
|
|||
}
|
||||
}, [isAuthenticated]);
|
||||
if (!isAuthenticated && autoLogin !== undefined && !autoLogin) {
|
||||
return <CustomNavigate to="/login" replace />;
|
||||
const currentPath = window.location.pathname;
|
||||
const isHomePath = currentPath === "/" || currentPath === "/flows";
|
||||
const isLoginPage = location.pathname.includes("login");
|
||||
return (
|
||||
<CustomNavigate
|
||||
to={
|
||||
"/login" +
|
||||
(!isHomePath && !isLoginPage ? "?redirect=" + currentPath : "")
|
||||
}
|
||||
replace
|
||||
/>
|
||||
);
|
||||
} else {
|
||||
return children;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,14 @@ export const ProtectedLoginRoute = ({ children }) => {
|
|||
const autoLogin = useAuthStore((state) => state.autoLogin);
|
||||
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
||||
|
||||
if (autoLogin === true) {
|
||||
return <CustomNavigate to="/" replace />;
|
||||
}
|
||||
if (autoLogin === true || isAuthenticated) {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const redirectPath = urlParams.get("redirect");
|
||||
|
||||
if (isAuthenticated) {
|
||||
return <CustomNavigate to="/" replace />;
|
||||
if (redirectPath) {
|
||||
return <CustomNavigate to={redirectPath} replace />;
|
||||
}
|
||||
return <CustomNavigate to="/home" replace />;
|
||||
}
|
||||
|
||||
return children;
|
||||
|
|
|
|||
|
|
@ -44,7 +44,12 @@ export const useGetAutoLogin: useQueryFunctionType<undefined, undefined> = (
|
|||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue