feat(login): enhance routing by preserving 'from' path in login redirects
This commit is contained in:
@@ -10,6 +10,7 @@ function LoginForm() {
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
const justSetup = searchParams.get("setup") === "1";
|
const justSetup = searchParams.get("setup") === "1";
|
||||||
|
const fromPath = searchParams.get("from");
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
@@ -29,7 +30,7 @@ function LoginForm() {
|
|||||||
if (res.error) {
|
if (res.error) {
|
||||||
setError(res.error.message ?? "登录失败,请检查用户名和密码");
|
setError(res.error.message ?? "登录失败,请检查用户名和密码");
|
||||||
} else {
|
} else {
|
||||||
router.push("/dashboard");
|
router.push(fromPath ?? "/dashboard");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -47,7 +48,7 @@ function LoginForm() {
|
|||||||
if (res?.error) {
|
if (res?.error) {
|
||||||
setError(res.error.message ?? "Passkey 登录失败");
|
setError(res.error.message ?? "Passkey 登录失败");
|
||||||
} else {
|
} else {
|
||||||
router.push("/dashboard");
|
router.push(fromPath ?? "/dashboard");
|
||||||
router.refresh();
|
router.refresh();
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
|||||||
@@ -63,7 +63,8 @@ export async function middleware(request: NextRequest) {
|
|||||||
|
|
||||||
if (!sessionCookie) {
|
if (!sessionCookie) {
|
||||||
const loginUrl = new URL("/login", request.url);
|
const loginUrl = new URL("/login", request.url);
|
||||||
loginUrl.searchParams.set("from", pathname);
|
const fromPath = request.nextUrl.search ? pathname + request.nextUrl.search : pathname;
|
||||||
|
loginUrl.searchParams.set("from", fromPath);
|
||||||
const res = NextResponse.redirect(loginUrl);
|
const res = NextResponse.redirect(loginUrl);
|
||||||
if (!fromCache) res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
if (!fromCache) res.cookies.set("helios_setup_done", "1", { path: "/", httpOnly: true, sameSite: "lax" });
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user