fix(auth): read redirect on client
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* 原始 Dart: lib/ui/auth/auth_screen.dart
|
* 原始 Dart: lib/ui/auth/auth_screen.dart
|
||||||
*/
|
*/
|
||||||
import { useEffect } from "react";
|
import { useEffect, useSyncExternalStore } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
import { MobileShell } from "@/app/_components/core";
|
import { MobileShell } from "@/app/_components/core";
|
||||||
@@ -17,14 +17,15 @@ import { Logger } from "@/utils";
|
|||||||
|
|
||||||
const log = new Logger("AppAuthAuthScreen");
|
const log = new Logger("AppAuthAuthScreen");
|
||||||
|
|
||||||
export interface AuthScreenProps {
|
export function AuthScreen() {
|
||||||
redirectTo?: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AuthScreen({ redirectTo = null }: AuthScreenProps) {
|
|
||||||
const state = useAuthState();
|
const state = useAuthState();
|
||||||
const authDispatch = useAuthDispatch();
|
const authDispatch = useAuthDispatch();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const redirectTo = useSyncExternalStore(
|
||||||
|
subscribeLocationSnapshot,
|
||||||
|
readRedirectFromLocation,
|
||||||
|
() => null,
|
||||||
|
);
|
||||||
const safeRedirectTo = getSafeRedirectPath(redirectTo) ?? ROUTES.chat;
|
const safeRedirectTo = getSafeRedirectPath(redirectTo) ?? ROUTES.chat;
|
||||||
|
|
||||||
// 邮箱登录成功 → 跳转;User hydrate 由根级 <UserAuthSync /> 监听 loginStatus 变化处理。
|
// 邮箱登录成功 → 跳转;User hydrate 由根级 <UserAuthSync /> 监听 loginStatus 变化处理。
|
||||||
@@ -70,3 +71,12 @@ function getSafeRedirectPath(redirectTo: string | null | undefined): string | nu
|
|||||||
if (!redirectTo.startsWith("/") || redirectTo.startsWith("//")) return null;
|
if (!redirectTo.startsWith("/") || redirectTo.startsWith("//")) return null;
|
||||||
return redirectTo;
|
return redirectTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function readRedirectFromLocation(): string | null {
|
||||||
|
if (typeof window === "undefined") return null;
|
||||||
|
return new URLSearchParams(window.location.search).get("redirect");
|
||||||
|
}
|
||||||
|
|
||||||
|
function subscribeLocationSnapshot(): () => void {
|
||||||
|
return () => {};
|
||||||
|
}
|
||||||
|
|||||||
+2
-13
@@ -1,16 +1,5 @@
|
|||||||
import { AuthScreen } from "@/app/auth/auth-screen";
|
import { AuthScreen } from "@/app/auth/auth-screen";
|
||||||
|
|
||||||
interface AuthPageProps {
|
export default function AuthPage() {
|
||||||
searchParams: Promise<{
|
return <AuthScreen />;
|
||||||
redirect?: string | string[];
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function AuthPage({ searchParams }: AuthPageProps) {
|
|
||||||
const params = await searchParams;
|
|
||||||
const redirectTo = Array.isArray(params.redirect)
|
|
||||||
? params.redirect[0]
|
|
||||||
: params.redirect;
|
|
||||||
|
|
||||||
return <AuthScreen redirectTo={redirectTo ?? null} />;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user