mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
feat: Improve sign-in error handling and redirect flow
This commit is contained in:
parent
5c2085d238
commit
fc347747c4
@ -1,6 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
import { signIn } from "@/actions/user-authorize";
|
import { signIn } from "@/actions/user-authorize";
|
||||||
import { useSession } from "next-auth/react";
|
import { useSession } from "next-auth/react";
|
||||||
|
|
||||||
@ -8,6 +9,11 @@ interface UserAuthorizeProps extends React.HTMLAttributes<HTMLDivElement> {
|
|||||||
data: Record<string, any>;
|
data: Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface SignInResult {
|
||||||
|
error?: string;
|
||||||
|
ok?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export function UserAuthorize({
|
export function UserAuthorize({
|
||||||
className,
|
className,
|
||||||
data,
|
data,
|
||||||
@ -16,6 +22,7 @@ export function UserAuthorize({
|
|||||||
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 { update } = useSession();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
const signInCallback = useCallback(async () => {
|
const signInCallback = useCallback(async () => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@ -23,15 +30,21 @@ export function UserAuthorize({
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await signIn({ ...data, redirectTo: "/dashboard" });
|
const result = (await signIn({ ...data })) as SignInResult;
|
||||||
// 更新 session
|
if (result?.error) {
|
||||||
await update();
|
setError(result.error);
|
||||||
setIsLoading(false);
|
} else {
|
||||||
|
// 更新 session
|
||||||
|
await update();
|
||||||
|
// 登录成功后重定向到控制台
|
||||||
|
router.push("/dashboard");
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(error);
|
setError(error);
|
||||||
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [data, isLoading, update]);
|
}, [data, isLoading, update, router]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
signInCallback();
|
signInCallback();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user