refactor(auth): remove AuthGate abstraction in favor of useSession
- Drop AuthGate/AuthGateState re-exports from @/lib/auth/nextauth - Simplify SplashButton by removing skipHref prop and hardcoding ROUTES.chat - Update comments to reference useSession() (next-auth/react) directly instead of the AuthGate wrapper, since the auth UI gate no longer needs the extra abstraction layer.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* `Row(children: [Skip 文本, SizedBox 26, Facebook 登录按钮])`
|
||||
*
|
||||
* 本组件是 splash 鉴权流程的**自治单元**:
|
||||
* - 消费 `useAuthGate`(内部用 useSession 监听 NextAuth session)
|
||||
* - 消费 `useSession()`(next-auth/react)监听 NextAuth session
|
||||
* - 消费 `useAuthState` / `useAuthDispatch`(邮箱登录流程)
|
||||
* - 消费 `useChatDispatch` / `useUserDispatch`(登录成功后初始化)
|
||||
* - 自管路由跳转(已登录 / 登录成功 → /chat)
|
||||
@@ -25,12 +25,7 @@ import { ROUTES } from "@/router/routes";
|
||||
import { LoadingIndicator } from "@/app/_components/core/loading-indicator";
|
||||
import styles from "./splash-button.module.css";
|
||||
|
||||
export interface SplashButtonProps {
|
||||
/** Skip 链接的目标路由(默认 /chat) */
|
||||
skipHref?: string;
|
||||
}
|
||||
|
||||
export function SplashButton({ skipHref = ROUTES.chat }: SplashButtonProps) {
|
||||
export function SplashButton() {
|
||||
// ===== 鉴权路由跳转已由 src/router/proxy.ts 集中处理 =====
|
||||
// 本组件只负责:
|
||||
// 1. 触发登录动作
|
||||
@@ -68,7 +63,7 @@ export function SplashButton({ skipHref = ROUTES.chat }: SplashButtonProps) {
|
||||
<div className={styles.wrapper}>
|
||||
{/* Skip 文本链接(next/link 渲染为 <a>,保留 SPA 体验) */}
|
||||
<Link
|
||||
href={skipHref}
|
||||
href={ROUTES.chat}
|
||||
className={styles.skip}
|
||||
aria-disabled={isLoading}
|
||||
tabIndex={isLoading ? -1 : 0}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
"use client";
|
||||
/**
|
||||
* 客户端鉴权门(class-oriented 封装)
|
||||
*
|
||||
* `useState` 是 static 方法而非实例方法(hooks 必须在 React 函数组件中调用,
|
||||
* static 方法本质上仍由组件调用,hook 链合法)。调用方写法:
|
||||
*
|
||||
* ```tsx
|
||||
* function MyComponent() {
|
||||
* const { isAuthed } = AuthGate.useState();
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* 原始 Dart: nextauth-helpers.useAuthGate()
|
||||
*/
|
||||
import { useSession } from "next-auth/react";
|
||||
|
||||
export interface AuthGateState {
|
||||
isAuthed: boolean;
|
||||
}
|
||||
|
||||
export class AuthGate {
|
||||
/** 必须在 React 函数组件内调用(useSession 是 hook) */
|
||||
static useState(): AuthGateState {
|
||||
const { data: session } = useSession();
|
||||
return { isAuthed: Boolean(session?.user) };
|
||||
}
|
||||
}
|
||||
@@ -43,9 +43,8 @@ export { handler as GET, handler as POST };
|
||||
// 每个 class 一个独立文件,详见同目录的子模块。
|
||||
// 这里 re-export 仅为消费者提供 `@/lib/auth/nextauth` 单点导入。
|
||||
// 注:上方 `next-auth` 是 server-only,本文件编译时会被 Next.js 切分到 server bundle;
|
||||
// 客户端 import `GoogleLogin` / `FacebookLogin` / `Logout` / `AuthGate` 时不会拉入。
|
||||
// 客户端 import `GoogleLogin` / `FacebookLogin` / `Logout` 时不会拉入。
|
||||
// ============================================================
|
||||
export { GoogleLogin } from "./google_login";
|
||||
export { FacebookLogin } from "./facebook_login";
|
||||
export { Logout } from "./logout";
|
||||
export { AuthGate, type AuthGateState } from "./auth_gate";
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@
|
||||
* 重要约束:
|
||||
* - **不**做自定义 cookie 持久化:统一用 NextAuth 内置 `next-auth.session-token` /
|
||||
* `__Secure-next-auth.session-token`(v5 默认 httpOnly + secure)作信号。
|
||||
* - 真正的鉴权门在 Client 侧 `AuthGate.useState()`(用 `useSession()`),用于 UI 状态。
|
||||
* - 真正的鉴权门在 Client 侧 `useSession()`(next-auth/react),用于 UI 状态。
|
||||
* - 路由跳转(**唯一**鉴权跳转点)由本 proxy 集中处理。
|
||||
* - 副作用:邮箱登录用户(不走 NextAuth)不会被本 proxy 识别为已登录。
|
||||
* 邮箱登录在后续轮次若要恢复 proxy 行为,需引入 NextAuth EmailProvider 或独立
|
||||
|
||||
Reference in New Issue
Block a user