refactor(auth): centralize auth routing via proxy and marker cookie

This commit is contained in:
2026-06-10 12:47:47 +08:00
parent 1db3e3ae31
commit 2066934094
7 changed files with 156 additions and 38 deletions
+8 -10
View File
@@ -17,10 +17,10 @@ import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef } from "react";
import { useAuthState, useAuthDispatch } from "@/stores/auth/auth-context";
import { useAuthState } from "@/stores/auth/auth-context";
import { useChatDispatch } from "@/stores/chat/chat-context";
import { useUserDispatch } from "@/stores/user/user-context";
import { facebookLogin, useAuthGate } from "@/lib/auth/nextauth";
import { facebookLogin } from "@/lib/auth/nextauth";
import { ROUTES } from "@/router/routes";
import { LoadingIndicator } from "@/app/_components/core/loading-indicator";
import styles from "./splash-button.module.css";
@@ -31,10 +31,13 @@ export interface SplashButtonProps {
}
export function SplashButton({ skipHref = ROUTES.chat }: SplashButtonProps) {
// ===== 鉴权状态 =====
const { isAuthed } = useAuthGate();
// ===== 鉴权路由跳转已由 src/router/proxy.ts 集中处理 =====
// 本组件只负责:
// 1. 触发登录动作
// 2. 登录成功后初始化 chat/user store + navigate 到 /chat(业务层动作,
// 不是鉴权判断;proxy 会让通过因为 cookie 已设)
const state = useAuthState();
const authDispatch = useAuthDispatch();
const isLoading = state.isLoading;
// ===== 跨 store 初始化(登录成功后触发) =====
@@ -43,11 +46,6 @@ export function SplashButton({ skipHref = ROUTES.chat }: SplashButtonProps) {
const router = useRouter();
const wasSuccess = useRef(false);
// 已登录时跳 /chat
useEffect(() => {
if (isAuthed) router.replace(ROUTES.chat);
}, [isAuthed, router]);
// 邮箱登录成功跳 /chat + 通知 chat/user 初始化
useEffect(() => {
if (state.isSuccess && !wasSuccess.current) {