diff --git a/src/components/auth/user-authorize.tsx b/src/components/auth/user-authorize.tsx index 7539573..9fdbfbf 100644 --- a/src/components/auth/user-authorize.tsx +++ b/src/components/auth/user-authorize.tsx @@ -1,6 +1,7 @@ "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"; @@ -8,6 +9,11 @@ interface UserAuthorizeProps extends React.HTMLAttributes { data: Record; } +interface SignInResult { + error?: string; + ok?: boolean; +} + export function UserAuthorize({ className, data, @@ -16,6 +22,7 @@ export function UserAuthorize({ const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const { update } = useSession(); + const router = useRouter(); const signInCallback = useCallback(async () => { if (isLoading) { @@ -23,15 +30,21 @@ export function UserAuthorize({ } setIsLoading(true); try { - const result = await signIn({ ...data, redirectTo: "/dashboard" }); - // 更新 session - await update(); - setIsLoading(false); + const result = (await signIn({ ...data })) as SignInResult; + if (result?.error) { + setError(result.error); + } else { + // 更新 session + await update(); + // 登录成功后重定向到控制台 + router.push("/dashboard"); + } } catch (error) { setError(error); + } finally { setIsLoading(false); } - }, [data, isLoading, update]); + }, [data, isLoading, update, router]); useEffect(() => { signInCallback();