refactor(auth): simplify splash guest entry

This commit is contained in:
2026-07-03 15:57:01 +08:00
parent 79324defcf
commit b22f23bcc4
10 changed files with 72 additions and 205 deletions
+9 -14
View File
@@ -7,7 +7,8 @@
import { useEffect, useSyncExternalStore } from "react";
import { MobileShell } from "@/app/_components/core";
import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context";
import { useAuthState } from "@/stores/auth/auth-context";
import { isAuthenticatedUser } from "@/router/navigation-resolver";
import { ROUTES } from "@/router/routes";
import { useAppNavigator } from "@/router/use-app-navigator";
@@ -19,7 +20,6 @@ const log = new Logger("AppAuthAuthScreen");
export function AuthScreen() {
const state = useAuthState();
const authDispatch = useAuthDispatch();
const navigator = useAppNavigator();
const redirectTo = useSyncExternalStore(
subscribeLocationSnapshot,
@@ -28,28 +28,23 @@ export function AuthScreen() {
);
const safeRedirectTo = getSafeRedirectPath(redirectTo) ?? ROUTES.chat;
// 邮箱登录成功 → 跳转;User hydrate 由根级 <UserAuthSync /> 监听 loginStatus 变化处理。
// 用 pendingRedirect flag(不是 useRef transition detection)——
// re-mount 时 flag 在 auth context 里持久,区分"刚按了"和"re-visit"准确无误。
// 真实用户登录成功 → 跳转;User hydrate 由根级 <UserAuthSync /> 监听 loginStatus 变化处理。
// 游客态不触发跳转,避免自动游客登录打断 auth 页面。
useEffect(() => {
log.debug("[auth-screen] useEffect (loginStatus + pendingRedirect)", {
pending: state.pendingRedirect,
const shouldRedirect = isAuthenticatedUser(state.loginStatus);
log.debug("[auth-screen] useEffect (loginStatus)", {
loginStatus: state.loginStatus,
willRedirect:
state.pendingRedirect && state.loginStatus !== "notLoggedIn",
willRedirect: shouldRedirect,
});
if (state.pendingRedirect && state.loginStatus !== "notLoggedIn") {
log.debug("[auth-screen] useEffect → navigator.replace [via pendingRedirect flag]", {
if (shouldRedirect) {
log.debug("[auth-screen] useEffect → navigator.replace", {
redirectTo: safeRedirectTo,
});
authDispatch({ type: "AuthClearPendingRedirect" });
navigator.replace(safeRedirectTo);
}
}, [
state.loginStatus,
state.pendingRedirect,
authDispatch,
navigator,
safeRedirectTo,
]);