refactor(auth): unify Facebook and Google login into AuthPlatform class

Replace the separate FacebookLogin and GoogleLogin classes with a single
AuthPlatform class that takes a provider name ("facebook" | "google") as a
constructor argument. This consolidates duplicate OAuth sign-in logic behind
one entry point while preserving the existing NextAuth flow.

- Add new src/lib/auth/auth_platform.ts exporting AuthPlatform and
  AuthProvider type
- Update auth-facebook-panel.tsx and splash-button.tsx to use the new API
- Rename initialContext → initialState in the auth machine for consistency
- Update inline docs and comments to reference AuthPlatform

No behavioral change for end users; both providers still route through
NextAuth's signIn() with the same callbacks and cookies.
This commit is contained in:
2026-06-10 18:58:15 +08:00
parent 744e23fc29
commit 47591be41c
30 changed files with 111 additions and 117 deletions
+31
View File
@@ -0,0 +1,31 @@
"use client";
/**
* AuthPlatform 统一 OAuth 登录入口
*
* 合并自原 FacebookLogin / GoogleLogin 两个类。
* 构造时传入 provider 名,调用 signIn() 触发 NextAuth OAuth 流程。
*
* 用法:
* await new AuthPlatform("facebook").signIn()
* await new AuthPlatform("google").signIn()
*
* NextAuth 流程:
* 1. NextAuth 重定向到 provider OAuth 授权页
* 2. 用户授权 → 回调 /api/auth/callback/{provider}
* 3. NextAuth 写 next-auth.session-token (httpOnly) cookie
* 4. 重定向到 callbackUrl (默认 /chat)
*
* **不**做任何持久化(不写 unstorage / 不调后端 / 不设自定义 cookie)。
* 原始 Dart: nextauth-helpers.{facebookLogin, googleLogin}()
*/
import { signIn } from "next-auth/react";
export type AuthProvider = "facebook" | "google";
export class AuthPlatform {
constructor(private readonly provider: AuthProvider) {}
async signIn(): Promise<void> {
await signIn(this.provider);
}
}
-20
View File
@@ -1,20 +0,0 @@
"use client";
/**
* Facebook 登录(NextAuth OAuth 流程触发器)
*
* 调用 `signIn("facebook")` 触发 NextAuth 的 OAuth 流程:
* 1. NextAuth 重定向到 Facebook OAuth 授权页
* 2. 用户授权 → Facebook 回调 /api/auth/callback/facebook
* 3. NextAuth 写 `next-auth.session-token` (httpOnly) cookie
* 4. 重定向到 callbackUrl (默认 /chat)
*
* **不**做任何持久化(不写 unstorage / 不调后端 / 不设自定义 cookie)。
* 原始 Dart: nextauth-helpers.facebookLogin()
*/
import { signIn } from "next-auth/react";
export class FacebookLogin {
async signIn(): Promise<void> {
await signIn("facebook");
}
}
-20
View File
@@ -1,20 +0,0 @@
"use client";
/**
* Google 登录(NextAuth OAuth 流程触发器)
*
* 调用 `signIn("google")` 触发 NextAuth 的 OAuth 流程:
* 1. NextAuth 重定向到 Google OAuth 授权页
* 2. 用户授权 → Google 回调 /api/auth/callback/google
* 3. NextAuth 写 `next-auth.session-token` (httpOnly) cookie
* 4. 重定向到 callbackUrl (默认 /chat)
*
* **不**做任何持久化(不写 unstorage / 不调后端 / 不设自定义 cookie)。
* 原始 Dart: nextauth-helpers.googleLogin()
*/
import { signIn } from "next-auth/react";
export class GoogleLogin {
async signIn(): Promise<void> {
await signIn("google");
}
}
+4 -5
View File
@@ -2,14 +2,14 @@
* NextAuth v4 配置(单一来源)
*
* 极简模式:参考 `auth.js` 官方示例的**精神**(不做任何持久化),但保留 v4 语法:
* - 客户端点击登录按钮 → `new GoogleLogin().signIn()` / `new FacebookLogin().signIn()`
* - 客户端点击登录按钮 → `new AuthPlatform("google").signIn()` / `new AuthPlatform("facebook").signIn()`
* - NextAuth 重定向到 Google / Facebook OAuth
* - OAuth 回调 → NextAuth 写 `next-auth.session-token` (httpOnly) cookie
* - 重定向到 callbackUrl (默认 /chat)
* - 业务 token 持久化(unstorage / 后端 / 自定义 cookie**全部不在本轮 scope**
*
* 导出 `GET` / `POST` 给 `src/app/api/auth/[...nextauth]/route.ts` 引用。
* 客户端通过 `src/lib/auth/{google_login,facebook_login,logout,auth_gate}.ts` 触发。
* 客户端通过 `src/lib/auth/{auth_platform,logout}.ts` 触发。
*/
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
@@ -43,8 +43,7 @@ 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` 时不会拉入。
// 客户端 import `AuthPlatform` / `Logout` 时不会拉入。
// ============================================================
export { GoogleLogin } from "./google_login";
export { FacebookLogin } from "./facebook_login";
export { AuthPlatform, type AuthProvider } from "./auth_platform";
export { Logout } from "./logout";