refactor(auth): centralize login status sync
This commit is contained in:
@@ -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 });
|
||||
|
||||
Reference in New Issue
Block a user