mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
refactor: 优化用户授权组件的错误处理逻辑,简化代码结构
This commit is contained in:
parent
a5a9f2e779
commit
0ef9ae7903
@ -1,9 +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 { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
|
||||||
@ -18,8 +16,6 @@ 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) {
|
||||||
@ -27,26 +23,13 @@ export function UserAuthorize({
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
// 从 URL 中获取 sso 和 sig 参数
|
await signIn(data);
|
||||||
const url = new URL(window.location.href);
|
|
||||||
const sso = url.searchParams.get("sso");
|
|
||||||
const sig = url.searchParams.get("sig");
|
|
||||||
|
|
||||||
if (!sso || !sig) {
|
|
||||||
throw new Error("缺少必要的认证参数");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 直接调用signIn,它会处理SSO回调中的return_sso_url
|
|
||||||
await signIn({ sso, sig });
|
|
||||||
// 更新session状态
|
|
||||||
await update();
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("登录过程出错:", error);
|
|
||||||
setError(error);
|
setError(error);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [isLoading, update]);
|
}, [data, isLoading]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timer = setTimeout(signInCallback, 5);
|
const timer = setTimeout(signInCallback, 5);
|
||||||
@ -55,41 +38,43 @@ export function UserAuthorize({
|
|||||||
};
|
};
|
||||||
}, [signInCallback]);
|
}, [signInCallback]);
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return (
|
|
||||||
<Card className="w-full">
|
|
||||||
<CardHeader className="space-y-4 text-center">
|
|
||||||
<div className="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-red-100">
|
|
||||||
<div className="h-8 w-8 text-red-600">❌</div>
|
|
||||||
</div>
|
|
||||||
<CardTitle className="text-2xl font-semibold text-red-600">
|
|
||||||
授权失败
|
|
||||||
</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<p className="text-center text-gray-500">
|
|
||||||
{error instanceof Error
|
|
||||||
? error.message
|
|
||||||
: "登录异常,授权失败!请稍后重试。"}
|
|
||||||
</p>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="w-full">
|
<>
|
||||||
<CardHeader className="space-y-4 text-center">
|
{error ? (
|
||||||
<div className="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-blue-100">
|
<Card className="w-full">
|
||||||
<div className="h-8 w-8 animate-spin rounded-full border-4 border-blue-600 border-t-transparent"></div>
|
<CardHeader className="space-y-4 text-center">
|
||||||
</div>
|
<div className="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-red-100">
|
||||||
<CardTitle className="text-2xl font-semibold">正在处理授权</CardTitle>
|
<div className="h-8 w-8 text-red-600">❌</div>
|
||||||
</CardHeader>
|
</div>
|
||||||
<CardContent>
|
<CardTitle className="text-2xl font-semibold text-red-600">
|
||||||
<p className="text-center text-gray-500">
|
授权失败
|
||||||
{isLoading ? "请稍候,我们正在处理您的授权请求" : "正在跳转..."}
|
</CardTitle>
|
||||||
</p>
|
</CardHeader>
|
||||||
</CardContent>
|
<CardContent>
|
||||||
</Card>
|
<p className="text-center text-gray-500">
|
||||||
|
{error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "登录异常,授权失败!请稍后重试。"}
|
||||||
|
</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
) : (
|
||||||
|
<Card className="w-full">
|
||||||
|
<CardHeader className="space-y-4 text-center">
|
||||||
|
<div className="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-blue-100">
|
||||||
|
<div className="h-8 w-8 animate-spin rounded-full border-4 border-blue-600 border-t-transparent"></div>
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl font-semibold">
|
||||||
|
正在处理授权
|
||||||
|
</CardTitle>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p className="text-center text-gray-500">
|
||||||
|
{isLoading ? "请稍候,我们正在处理您的授权请求" : "正在跳转..."}
|
||||||
|
</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user