mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
refactor: 更新用户授权组件以使用next-auth进行登录,简化重定向逻辑并优化会话更新
This commit is contained in:
parent
0ef9ae7903
commit
0c98d52a16
@ -1,7 +1,8 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
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";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
|
||||||
@ -16,6 +17,8 @@ export function UserAuthorize({
|
|||||||
}: UserAuthorizeProps) {
|
}: UserAuthorizeProps) {
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
const [error, setError] = useState<Error | unknown>(null);
|
const [error, setError] = useState<Error | unknown>(null);
|
||||||
|
const { update } = useSession();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
const signInCallback = useCallback(async () => {
|
const signInCallback = useCallback(async () => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@ -23,19 +26,28 @@ export function UserAuthorize({
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
await signIn(data);
|
const result = await nextAuthSignIn("credentials", {
|
||||||
setIsLoading(false);
|
...data,
|
||||||
|
redirect: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result?.error) {
|
||||||
|
setError(result.error);
|
||||||
|
} else {
|
||||||
|
// 更新 session
|
||||||
|
await update();
|
||||||
|
// 登录成功后重定向到控制台
|
||||||
|
router.push("/dashboard");
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(error);
|
setError(error);
|
||||||
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [data, isLoading]);
|
}, [data, isLoading, update, router]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setTimeout(signInCallback, 5);
|
signInCallback();
|
||||||
return () => {
|
|
||||||
clearTimeout(timer);
|
|
||||||
};
|
|
||||||
}, [signInCallback]);
|
}, [signInCallback]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user