refactor(auth): remove unused machine state
This commit is contained in:
@@ -1,11 +0,0 @@
|
|||||||
/**
|
|
||||||
* 认证模式(登录 / 注册)
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
export const AuthMode = {
|
|
||||||
Login: "login",
|
|
||||||
Register: "register",
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
export type AuthMode = (typeof AuthMode)[keyof typeof AuthMode];
|
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
* @file Automatically generated by barrelsby.
|
* @file Automatically generated by barrelsby.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export * from "./auth_mode";
|
|
||||||
export * from "./auth_panel_mode";
|
export * from "./auth_panel_mode";
|
||||||
export * from "./facebook_user_data";
|
export * from "./facebook_user_data";
|
||||||
export * from "./login_status";
|
export * from "./login_status";
|
||||||
|
|||||||
@@ -58,22 +58,12 @@ function createTestAuthMachine(
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("authMachine", () => {
|
describe("authMachine", () => {
|
||||||
it("updates panel mode, auth mode, and unsupported Apple login errors", () => {
|
it("updates panel mode", () => {
|
||||||
const actor = createActor(createTestAuthMachine()).start();
|
const actor = createActor(createTestAuthMachine()).start();
|
||||||
|
|
||||||
actor.send({ type: "AuthPanelModeChanged", mode: "email" });
|
actor.send({ type: "AuthPanelModeChanged", mode: "email" });
|
||||||
actor.send({ type: "AuthModeChanged", mode: "register" });
|
|
||||||
|
|
||||||
expect(actor.getSnapshot().context.authPanelMode).toBe("email");
|
expect(actor.getSnapshot().context.authPanelMode).toBe("email");
|
||||||
expect(actor.getSnapshot().context.authMode).toBe("register");
|
|
||||||
|
|
||||||
actor.send({ type: "AuthAppleLoginSubmitted" });
|
|
||||||
expect(actor.getSnapshot().context.errorMessage).toBe(
|
|
||||||
"Apple login not implemented",
|
|
||||||
);
|
|
||||||
|
|
||||||
actor.send({ type: "AuthFormCleared" });
|
|
||||||
expect(actor.getSnapshot().context.errorMessage).toBeNull();
|
|
||||||
|
|
||||||
actor.stop();
|
actor.stop();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -25,11 +25,6 @@ import type { AuthEvent, AuthState as MachineContext } from "./auth-machine";
|
|||||||
*/
|
*/
|
||||||
interface AuthState {
|
interface AuthState {
|
||||||
authPanelMode: MachineContext["authPanelMode"];
|
authPanelMode: MachineContext["authPanelMode"];
|
||||||
authMode: MachineContext["authMode"];
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
username: string;
|
|
||||||
confirmPassword: string;
|
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
errorMessage: string | null;
|
errorMessage: string | null;
|
||||||
/** 当前登录状态(未登录 / 游客 / OAuth / 邮箱) */
|
/** 当前登录状态(未登录 / 游客 / OAuth / 邮箱) */
|
||||||
@@ -52,11 +47,6 @@ export function AuthProvider({ children }: AuthProviderProps) {
|
|||||||
const authState = useMemo<AuthState>(
|
const authState = useMemo<AuthState>(
|
||||||
() => ({
|
() => ({
|
||||||
authPanelMode: state.context.authPanelMode,
|
authPanelMode: state.context.authPanelMode,
|
||||||
authMode: state.context.authMode,
|
|
||||||
email: state.context.email,
|
|
||||||
password: state.context.password,
|
|
||||||
username: state.context.username,
|
|
||||||
confirmPassword: state.context.confirmPassword,
|
|
||||||
// isLoading 覆盖邮箱登录 / 邮箱注册 / OAuth 跳转(NextAuth 重定向期间)/
|
// isLoading 覆盖邮箱登录 / 邮箱注册 / OAuth 跳转(NextAuth 重定向期间)/
|
||||||
// OAuth 回调后端 sync / 显式游客登录
|
// OAuth 回调后端 sync / 显式游客登录
|
||||||
isLoading:
|
isLoading:
|
||||||
|
|||||||
@@ -4,13 +4,11 @@
|
|||||||
* 事件名与原 Dart AuthEvent 对齐。
|
* 事件名与原 Dart AuthEvent 对齐。
|
||||||
*/
|
*/
|
||||||
import type { AuthProvider } from "@/lib/auth/auth_platform";
|
import type { AuthProvider } from "@/lib/auth/auth_platform";
|
||||||
import type { AuthMode, AuthPanelMode } from "@/data/dto/auth";
|
import type { AuthPanelMode } from "@/data/dto/auth";
|
||||||
|
|
||||||
export type AuthEvent =
|
export type AuthEvent =
|
||||||
// 内部 UI 事件
|
// 内部 UI 事件
|
||||||
| { type: "AuthPanelModeChanged"; mode: AuthPanelMode }
|
| { type: "AuthPanelModeChanged"; mode: AuthPanelMode }
|
||||||
| { type: "AuthModeChanged"; mode: AuthMode }
|
|
||||||
| { type: "AuthFormCleared" }
|
|
||||||
| { type: "AuthLogoutSubmitted" }
|
| { type: "AuthLogoutSubmitted" }
|
||||||
/** App 启动时一次性派发 —— 读 storage 把 loginStatus 同步到状态机 */
|
/** App 启动时一次性派发 —— 读 storage 把 loginStatus 同步到状态机 */
|
||||||
| { type: "AuthInit" }
|
| { type: "AuthInit" }
|
||||||
@@ -27,7 +25,6 @@ export type AuthEvent =
|
|||||||
// 社交登录 —— 状态机内部调 next-auth/react 的 signIn(provider)
|
// 社交登录 —— 状态机内部调 next-auth/react 的 signIn(provider)
|
||||||
| { type: "AuthGoogleLoginSubmitted"; provider: AuthProvider }
|
| { type: "AuthGoogleLoginSubmitted"; provider: AuthProvider }
|
||||||
| { type: "AuthFacebookLoginSubmitted"; provider: AuthProvider }
|
| { type: "AuthFacebookLoginSubmitted"; provider: AuthProvider }
|
||||||
| { type: "AuthAppleLoginSubmitted" }
|
|
||||||
// OAuth 回调后:把 OAuth provider token 转交后端换业务 token
|
// OAuth 回调后:把 OAuth provider token 转交后端换业务 token
|
||||||
// 由 <OAuthSessionSync /> 监听 useSession() 派发
|
// 由 <OAuthSessionSync /> 监听 useSession() 派发
|
||||||
| { type: "AuthGoogleSyncSubmitted"; idToken: string }
|
| { type: "AuthGoogleSyncSubmitted"; idToken: string }
|
||||||
|
|||||||
@@ -70,21 +70,6 @@ export const authMachine = setup({
|
|||||||
errorMessage: null,
|
errorMessage: null,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
AuthModeChanged: {
|
|
||||||
actions: assign({
|
|
||||||
authMode: ({ event }) => event.mode,
|
|
||||||
errorMessage: null,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
AuthFormCleared: {
|
|
||||||
actions: assign({
|
|
||||||
email: "",
|
|
||||||
password: "",
|
|
||||||
username: "",
|
|
||||||
confirmPassword: "",
|
|
||||||
errorMessage: null,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
AuthLogoutSubmitted: {
|
AuthLogoutSubmitted: {
|
||||||
guard: "canLogoutToGuest",
|
guard: "canLogoutToGuest",
|
||||||
target: "loggingOut",
|
target: "loggingOut",
|
||||||
@@ -100,11 +85,6 @@ export const authMachine = setup({
|
|||||||
AuthEmailRegisterSubmitted: "loadingEmailRegister",
|
AuthEmailRegisterSubmitted: "loadingEmailRegister",
|
||||||
AuthGoogleLoginSubmitted: "loadingOAuth",
|
AuthGoogleLoginSubmitted: "loadingOAuth",
|
||||||
AuthFacebookLoginSubmitted: "loadingOAuth",
|
AuthFacebookLoginSubmitted: "loadingOAuth",
|
||||||
AuthAppleLoginSubmitted: {
|
|
||||||
actions: assign({
|
|
||||||
errorMessage: "Apple login not implemented",
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
|
|
||||||
// OAuth 回调后 sync 入口 —— 由 <OAuthSessionSync /> 派发
|
// OAuth 回调后 sync 入口 —— 由 <OAuthSessionSync /> 派发
|
||||||
AuthGoogleSyncSubmitted: "syncingGoogleBackend",
|
AuthGoogleSyncSubmitted: "syncingGoogleBackend",
|
||||||
|
|||||||
@@ -1,17 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Auth 状态机:State 形状 + 初始值
|
* Auth 状态机:State 形状 + 初始值
|
||||||
*/
|
*/
|
||||||
import type { AuthMode, AuthPanelMode, LoginStatus } from "@/data/dto/auth";
|
import type { AuthPanelMode, LoginStatus } from "@/data/dto/auth";
|
||||||
|
|
||||||
export interface AuthState {
|
export interface AuthState {
|
||||||
/** 当前面板模式(Facebook / Email) */
|
/** 当前面板模式(Facebook / Email) */
|
||||||
authPanelMode: AuthPanelMode;
|
authPanelMode: AuthPanelMode;
|
||||||
/** 内部登录模式(login / register) */
|
|
||||||
authMode: AuthMode;
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
username: string;
|
|
||||||
confirmPassword: string;
|
|
||||||
errorMessage: string | null;
|
errorMessage: string | null;
|
||||||
/** 当前登录状态(未登录 / 游客 / OAuth / 邮箱)—— 下游同步器监听此字段的双向变化 */
|
/** 当前登录状态(未登录 / 游客 / OAuth / 邮箱)—— 下游同步器监听此字段的双向变化 */
|
||||||
loginStatus: LoginStatus;
|
loginStatus: LoginStatus;
|
||||||
@@ -21,11 +15,6 @@ export interface AuthState {
|
|||||||
|
|
||||||
export const initialState: AuthState = {
|
export const initialState: AuthState = {
|
||||||
authPanelMode: "facebook",
|
authPanelMode: "facebook",
|
||||||
authMode: "login",
|
|
||||||
email: "",
|
|
||||||
password: "",
|
|
||||||
username: "",
|
|
||||||
confirmPassword: "",
|
|
||||||
errorMessage: null,
|
errorMessage: null,
|
||||||
loginStatus: "notLoggedIn",
|
loginStatus: "notLoggedIn",
|
||||||
hasInitialized: false,
|
hasInitialized: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user