diff --git a/adminfront/src/features/auth/AuthCallbackPage.tsx b/adminfront/src/features/auth/AuthCallbackPage.tsx index ed8889d8..ed1e0630 100644 --- a/adminfront/src/features/auth/AuthCallbackPage.tsx +++ b/adminfront/src/features/auth/AuthCallbackPage.tsx @@ -14,7 +14,14 @@ function AuthCallbackPage() { if (user?.access_token) { window.localStorage.setItem("admin_session", user.access_token); } - navigate("/", { replace: true }); + const returnTo = + typeof auth.user?.state === "object" && + auth.user?.state !== null && + "returnTo" in auth.user.state && + typeof auth.user.state.returnTo === "string" + ? auth.user.state.returnTo + : "/"; + navigate(returnTo, { replace: true }); } else if (auth.error) { console.error("Auth Error:", auth.error); navigate("/login", { replace: true }); diff --git a/adminfront/src/features/auth/LoginPage.tsx b/adminfront/src/features/auth/LoginPage.tsx index 07da4600..a9edebb5 100644 --- a/adminfront/src/features/auth/LoginPage.tsx +++ b/adminfront/src/features/auth/LoginPage.tsx @@ -1,5 +1,7 @@ import { ExternalLink, LogIn, ShieldHalf } from "lucide-react"; +import { useEffect, useRef } from "react"; import { useAuth } from "react-oidc-context"; +import { useNavigate, useSearchParams } from "react-router-dom"; import { Button } from "../../components/ui/button"; import { Card, @@ -11,10 +13,46 @@ import { function LoginPage() { const auth = useAuth(); + const navigate = useNavigate(); + const [searchParams] = useSearchParams(); + const autoStartedRef = useRef(false); + const returnTo = searchParams.get("returnTo") || "/"; + const shouldAutoLogin = searchParams.get("auto") === "1"; + + useEffect(() => { + if (auth.isAuthenticated) { + navigate(returnTo, { replace: true }); + } + }, [auth.isAuthenticated, navigate, returnTo]); + + useEffect(() => { + if (!shouldAutoLogin) { + return; + } + if (autoStartedRef.current || auth.isLoading || auth.activeNavigator) { + return; + } + + autoStartedRef.current = true; + void auth.signinRedirect({ + state: { + returnTo, + }, + }); + }, [ + auth, + auth.activeNavigator, + auth.isLoading, + returnTo, + shouldAutoLogin, + ]); const handleSSOLogin = () => { - // OIDC client-side authentication flow started here - auth.signinRedirect(); + void auth.signinRedirect({ + state: { + returnTo: "/", + }, + }); }; return (