refactor(auth): rename AuthStatusCheckSubmitted → AuthInit (init-only)

The previous event name implied "re-check status", but the actual
semantics is just "read storage on app start and sync loginStatus into
the machine". It was being dispatched from three sites:

1. AuthStatusChecker mount useEffect (the legit one-time init)
2. AuthStatusChecker loginStatus-watching useEffect (dead code — the
   machine's onDone already writes loginStatus, re-reading storage
   just returns the same value as a no-op)
3. Sidebar post-logout effect (also dead code — AuthReset directly
   sets loginStatus to initialState's "notLoggedIn", and
   userLogoutActor already cleared storage, so the values are
   already aligned)

This commit:

- Renames event AuthStatusCheckSubmitted → AuthInit in:
  * auth-events.ts (type union)
  * auth-machine.ts (handler + transition target: checkingAuthStatus →
    initializing, mirroring UserInit / initializing in user-machine)
  * auth-status-checker.tsx (single dispatch site)
  * root-providers.tsx (one comment)

- Simplifies auth-status-checker.tsx from 74 → ~40 lines:
  * Removes useEffect ② (loginStatus-watching re-check)
  * Removes prevLoginStatusRef + skipNextChangeRef + useAuthState
    (dead loop-guard machinery no longer needed)

- Removes the post-logout re-check dispatch from sidebar-screen.tsx:
  * AuthReset alone is sufficient — userLogoutActor cleared storage
    and AuthReset writes back initialState, so they match by
    construction. No re-verification needed.

Net: -44 lines, no behavior change for the happy paths (startup,
login, logout), one fewer source of false re-checks.
This commit is contained in:
2026-06-17 09:52:45 +08:00
parent 2acc005809
commit e85963e7bf
5 changed files with 25 additions and 69 deletions
@@ -51,12 +51,10 @@ export function SidebarScreen() {
const isNowLoggedOut = user.currentUser == null;
prevUserRef.current = user.currentUser;
if (wasLoggedIn && isNowLoggedOut) {
// 1) AuthReset:清场(清 email/password/username 等敏感字段
// AuthReset:清场(context 回到 initialStateloginStatus="notLoggedIn"
// 不需要再 dispatch AuthInit 校验 storage —— userLogoutActor 已清空 storage
// AuthReset 直接写回 initialState 已与 storage 对齐。
authDispatch({ type: "AuthReset" });
// 2) AuthStatusCheckSubmitted:通过 storage 重新验证实际 auth 状态
// userLogoutActor 已清空 userStoragecheckAuthStatusActor 会返 "notLoggedIn"
// 这一步确保 redirect 到 /chat 后,chat-screen 看到的是真实 storage 状态而非 AuthReset 直接置的 initialState
authDispatch({ type: "AuthStatusCheckSubmitted" });
router.replace(ROUTES.chat);
}
}, [user.currentUser, authDispatch, router]);