feat: 优化 OAuth 认证流程,改进参数获取和加载状态管理

This commit is contained in:
wood chen 2025-02-21 18:00:42 +08:00
parent 178d2af9aa
commit 0d1228ec30
2 changed files with 18 additions and 6 deletions

View File

@ -31,11 +31,20 @@ export function UserAuthForm({
const signIn = () => {
React.startTransition(async () => {
try {
setIsLoading(true);
// 构建请求体,包含 OAuth 参数
const body: Record<string, any> = {};
// 优先使用 URL 中的参数
if (searchParams?.toString()) {
body.oauth_params = searchParams.toString();
} else {
// 如果 URL 中没有参数,尝试从 localStorage 获取
const savedParams = localStorage.getItem("oauth_params");
if (savedParams) {
body.oauth_params = savedParams;
// 使用后清除存储的参数
localStorage.removeItem("oauth_params");
}
}
const response = await fetch("/api/auth/q58", {

View File

@ -1,6 +1,7 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { signIn } from "@/actions/user-authorize";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
@ -16,6 +17,7 @@ export function UserAuthorize({
}: UserAuthorizeProps) {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | unknown>(null);
const router = useRouter();
const signInCallback = useCallback(async () => {
if (isLoading) {
@ -32,20 +34,21 @@ export function UserAuthorize({
throw new Error("缺少必要的认证参数");
}
// 传递 SSO 参数和原始的 search 参数
// 传递 SSO 参数
await signIn({
sso,
sig,
returnTo: url.search, // 保存完整的查询参数
});
// 登录成功后刷新路由状态
router.refresh();
setIsLoading(false);
} catch (error) {
console.error("登录过程出错:", error);
setError(error);
setIsLoading(false);
}
}, [isLoading]);
}, [isLoading, router]);
useEffect(() => {
signInCallback();