fix(auth): redirect to splash after logout
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
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";
|
||||
@@ -36,15 +37,24 @@ export function SidebarScreen() {
|
||||
const auth = useAuthState();
|
||||
const authDispatch = useAuthDispatch();
|
||||
const router = useRouter();
|
||||
const isLogoutRedirectPendingRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
userDispatch({ type: "UserInit" });
|
||||
}, [userDispatch]);
|
||||
|
||||
const handleLogoutClick = () => {
|
||||
authDispatch({ type: "AuthReset" });
|
||||
userDispatch({ type: "UserLogout" });
|
||||
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 });
|
||||
};
|
||||
|
||||
// 状态派生:未登录 / 已登录未 VIP / 已登录 VIP
|
||||
|
||||
Reference in New Issue
Block a user