diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 3a4cd6e..10125c6 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -1,7 +1,9 @@ "use client"; import { useCallback, useEffect, useState } from "react"; +import { useRouter } from "next/navigation"; import { signIn } from "@/actions/user-authorize"; +import { useSession } from "next-auth/react"; interface UserAuthorizeProps extends React.HTMLAttributes { data: Record; @@ -14,6 +16,8 @@ export function UserAuthorize({ }: UserAuthorizeProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); + const { update } = useSession(); + const router = useRouter(); const signInCallback = useCallback(async () => { if (isLoading) { @@ -22,19 +26,22 @@ export function UserAuthorize({ setIsLoading(true); try { await signIn({ ...data, redirectTo: "/dashboard" }); + // 更新 session + await update(); + router.push("/dashboard"); setIsLoading(false); } catch (error) { setError(error); setIsLoading(false); } - }, []); + }, [data, isLoading, update, router]); useEffect(() => { const timer = setTimeout(signInCallback, 5); return () => { clearTimeout(timer); }; - }, []); + }, [signInCallback]); return ( <> diff --git a/src/components/layout/nav-bar.tsx b/src/components/layout/nav-bar.tsx index e76c365..8e21dbb 100644 --- a/src/components/layout/nav-bar.tsx +++ b/src/components/layout/nav-bar.tsx @@ -1,7 +1,9 @@ "use client"; +import { useEffect } from "react"; import Image from "next/image"; import Link from "next/link"; +import { useRouter } from "next/navigation"; import { User } from "lucide-react"; import { signOut, useSession } from "next-auth/react"; @@ -18,9 +20,15 @@ import { } from "../ui/dropdown-menu"; export function NavBar() { - const { data: session } = useSession(); + const { data: session, status } = useSession(); + const router = useRouter(); const user = session?.user; + const handleSignOut = async () => { + await signOut({ redirect: false }); + router.push("/"); + }; + return (