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 { FormEvent, useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { LockKeyhole, LogIn, Video } from "lucide-react";
|
||||
import { useAuth } from "@/components/auth/AuthProvider";
|
||||
import { readReturnToFromLocation } from "@/lib/auth-redirect";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
@@ -24,7 +25,7 @@ export default function LoginPage() {
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading && user) {
|
||||
router.replace("/");
|
||||
router.replace(readReturnToFromLocation());
|
||||
}
|
||||
}, [loading, router, user]);
|
||||
|
||||
@@ -34,7 +35,7 @@ export default function LoginPage() {
|
||||
setSubmitting(true);
|
||||
try {
|
||||
await login(username.trim(), password);
|
||||
router.replace("/");
|
||||
router.replace(readReturnToFromLocation());
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "登录失败");
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user