Refactor authentication redirection logic
- Introduce a new `auth-redirect` module to manage safe return paths for login and redirection. - Update the login page to utilize `readReturnToFromLocation` for redirecting users post-login. - Modify the `AuthGate` component to use `loginPathWithReturnTo` for redirecting unauthorized users, enhancing security and user experience. - Adjust API request handling to incorporate the new redirection logic, ensuring users are directed to the appropriate login page with return paths.
This commit is contained in:
@@ -4,6 +4,7 @@ import type { ReactNode } from "react";
|
||||
import { useEffect } from "react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { AppShell } from "@/components/layout/AppShell";
|
||||
import { loginPathWithReturnTo } from "@/lib/auth-redirect";
|
||||
import { useAuth } from "./AuthProvider";
|
||||
|
||||
export function AuthGate({ children }: { children: ReactNode }) {
|
||||
@@ -15,9 +16,9 @@ export function AuthGate({ children }: { children: ReactNode }) {
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading && !user && !isLoginPage) {
|
||||
router.replace("/login");
|
||||
router.replace(loginPathWithReturnTo(pathname));
|
||||
}
|
||||
}, [isLoginPage, loading, router, user]);
|
||||
}, [isLoginPage, loading, pathname, router, user]);
|
||||
|
||||
if (isLoginPage) {
|
||||
return <>{children}</>;
|
||||
|
||||
Reference in New Issue
Block a user