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:
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* Auth 状态机:Context 形状 + 初始值
|
||||
* Auth 状态机:State 形状 + 初始值
|
||||
*/
|
||||
import type { AuthMode } from "@/models/auth/auth-mode";
|
||||
import type { AuthPanelMode } from "@/models/auth/auth-panel-mode";
|
||||
import type { LoginType } from "@/models/auth/login-type";
|
||||
|
||||
export interface AuthContext {
|
||||
export interface AuthState {
|
||||
/** 当前面板模式(Facebook / Email) */
|
||||
authPanelMode: AuthPanelMode;
|
||||
/** 内部登录模式(login / register) */
|
||||
@@ -18,7 +18,7 @@ export interface AuthContext {
|
||||
loginType: LoginType;
|
||||
}
|
||||
|
||||
export const initialContext: AuthContext = {
|
||||
export const initialState: AuthState = {
|
||||
authPanelMode: "facebook",
|
||||
authMode: "login",
|
||||
email: "",
|
||||
@@ -7,4 +7,4 @@
|
||||
* - Context 形状与初始值由 `auth-context.ts` 导出
|
||||
* - `AuthState` 由 `auth-context.tsx` 导出(保持原 API 兼容)
|
||||
*/
|
||||
export type { AuthEvent } from "./auth-events";
|
||||
export type { AuthEvent } from "./machine/auth-events";
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
* - 事件联合位于 auth-events.ts
|
||||
* - machine / 类型 / React 入口分别由对应文件导出
|
||||
*/
|
||||
export * from "./auth-state";
|
||||
export * from "./auth-context";
|
||||
export * from "./auth-events";
|
||||
export * from "./auth-machine";
|
||||
export * from "./machine/auth-events";
|
||||
export * from "./machine/auth-machine";
|
||||
export * from "./auth-types";
|
||||
|
||||
@@ -18,12 +18,12 @@ import {
|
||||
import { useMachine } from "@xstate/react";
|
||||
|
||||
import { authMachine } from "./auth-machine";
|
||||
import type { AuthEvent, AuthContext as MachineContext } from "./auth-machine";
|
||||
import type { AuthEvent, AuthState as MachineContext } from "./auth-machine";
|
||||
|
||||
/**
|
||||
* 对外暴露的 State 形状(保持与原 AuthState 兼容)
|
||||
*/
|
||||
export interface AuthState {
|
||||
interface AuthState {
|
||||
authPanelMode: MachineContext["authPanelMode"];
|
||||
authMode: MachineContext["authMode"];
|
||||
email: string;
|
||||
@@ -4,8 +4,8 @@
|
||||
* 原始 Dart: lib/ui/auth/bloc/auth_bloc.dart + auth_state.dart + auth_event.dart
|
||||
*
|
||||
* 本轮迁移:社交登录改由 NextAuth 处理(`@/lib/auth/nextauth`),状态机仅保留
|
||||
* 邮箱注册/登录 + 通用 reset 事件。社交登录走 `new GoogleLogin().signIn()` /
|
||||
* `new FacebookLogin().signIn()`。
|
||||
* 邮箱注册/登录 + 通用 reset 事件。社交登录走 `new AuthPlatform("google").signIn()` /
|
||||
* `new AuthPlatform("facebook").signIn()`。
|
||||
*/
|
||||
import { setup, fromPromise, assign } from "xstate";
|
||||
|
||||
@@ -14,12 +14,12 @@ import { authRepository } from "@/data/repositories/auth_repository";
|
||||
import { AuthStorage } from "@/data/storage/auth/auth_storage";
|
||||
import { Result } from "@/utils/result";
|
||||
|
||||
import { AuthContext, initialContext } from "./auth-context";
|
||||
import { AuthState, initialState } from "../auth-state";
|
||||
import type { AuthEvent } from "./auth-events";
|
||||
|
||||
// 重新导出 Context / Event 类型,保持 machine 文件的公共 API 不变
|
||||
export type { AuthContext } from "./auth-context";
|
||||
export { initialContext } from "./auth-context";
|
||||
// 重新导出 State / Event 类型,保持 machine 文件的公共 API 不变
|
||||
export type { AuthState } from "../auth-state";
|
||||
export { initialState } from "../auth-state";
|
||||
export type { AuthEvent } from "./auth-events";
|
||||
|
||||
// ============================================================
|
||||
@@ -73,7 +73,7 @@ const emailRegisterThenLoginActor = fromPromise<
|
||||
// ============================================================
|
||||
export const authMachine = setup({
|
||||
types: {
|
||||
context: {} as AuthContext,
|
||||
context: {} as AuthState,
|
||||
events: {} as AuthEvent,
|
||||
},
|
||||
actors: {
|
||||
@@ -83,7 +83,7 @@ export const authMachine = setup({
|
||||
}).createMachine({
|
||||
id: "auth",
|
||||
initial: "idle",
|
||||
context: initialContext,
|
||||
context: initialState,
|
||||
states: {
|
||||
idle: {
|
||||
on: {
|
||||
@@ -109,11 +109,11 @@ export const authMachine = setup({
|
||||
}),
|
||||
},
|
||||
AuthReset: {
|
||||
actions: assign(() => initialContext),
|
||||
actions: assign(() => initialState),
|
||||
},
|
||||
AuthEmailLoginSubmitted: "loadingEmailLogin",
|
||||
AuthEmailRegisterSubmitted: "loadingEmailRegister",
|
||||
// 社交登录已迁移到 NextAuth + GoogleLogin / FacebookLogin 类
|
||||
// 社交登录已迁移到 NextAuth + AuthPlatform 类
|
||||
// 状态机保留事件以兼容旧调用方(实际不再触发)
|
||||
AuthGoogleLoginSubmitted: {},
|
||||
AuthFacebookLoginSubmitted: {},
|
||||
Reference in New Issue
Block a user