diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 5530211..8becf4a 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -1,7 +1,8 @@ "use client"; import { useCallback, useEffect, useState } from "react"; -import { signIn } from "@/actions/user-authorize"; +import { useRouter } from "next/navigation"; +import { signIn as nextAuthSignIn, useSession } from "next-auth/react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; @@ -16,6 +17,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) { @@ -23,19 +26,28 @@ export function UserAuthorize({ } setIsLoading(true); try { - await signIn(data); - setIsLoading(false); + const result = await nextAuthSignIn("credentials", { + ...data, + redirect: false, + }); + + if (result?.error) { + setError(result.error); + } else { + // 更新 session + await update(); + // 登录成功后重定向到控制台 + router.push("/dashboard"); + } } catch (error) { setError(error); + } finally { setIsLoading(false); } - }, [data, isLoading]); + }, [data, isLoading, update, router]); useEffect(() => { - const timer = setTimeout(signInCallback, 5); - return () => { - clearTimeout(timer); - }; + signInCallback(); }, [signInCallback]); return (