refactor(auth): centralize login status sync

This commit is contained in:
2026-06-18 12:52:49 +08:00
parent 0d0dabaace
commit f600e11d55
11 changed files with 115 additions and 128 deletions
-17
View File
@@ -1,11 +1,8 @@
"use client";
import { useEffect, useRef } from "react";
import { useRouter } from "next/navigation";
import { signOut } from "next-auth/react";
import { MobileShell, SettingsSection } from "@/app/_components/core";
import { ROUTES } from "@/router/routes";
import { useAuthDispatch, useAuthState } from "@/stores/auth/auth-context";
import { useUserDispatch, useUserState } from "@/stores/user/user-context";
@@ -36,22 +33,8 @@ export function SidebarScreen() {
const userDispatch = useUserDispatch();
const auth = useAuthState();
const authDispatch = useAuthDispatch();
const router = useRouter();
const isLogoutRedirectPendingRef = useRef(false);
useEffect(() => {
userDispatch({ type: "UserInit" });
}, [userDispatch]);
useEffect(() => {
if (!isLogoutRedirectPendingRef.current) return;
if (auth.isLoading || auth.loginStatus !== "notLoggedIn") return;
isLogoutRedirectPendingRef.current = false;
router.replace(ROUTES.splash);
}, [auth.isLoading, auth.loginStatus, router]);
const handleLogoutClick = () => {
isLogoutRedirectPendingRef.current = true;
userDispatch({ type: "UserClearLocal" });
authDispatch({ type: "AuthLogoutSubmitted" });
void signOut({ redirect: false });