mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
refactor: Simplify SSO authorization flow by removing OAuth parameter handling
This commit is contained in:
parent
a0026c005d
commit
4254098b53
@ -13,21 +13,8 @@ export async function POST(req: Request) {
|
||||
try {
|
||||
const nonce = WordArray.random(16).toString();
|
||||
|
||||
// 尝试从请求体中获取 OAuth 参数
|
||||
let oauthParams = "";
|
||||
try {
|
||||
const body = await req.json();
|
||||
if (body.oauth_params) {
|
||||
oauthParams = body.oauth_params;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to parse request body:", error);
|
||||
}
|
||||
|
||||
// 设置回调地址,如果有 OAuth 参数则回到授权页面
|
||||
const return_url = oauthParams
|
||||
? `${hostUrl}/authorize?${oauthParams}`
|
||||
: `${hostUrl}/dashboard`;
|
||||
// 设置基本的回调地址
|
||||
const return_url = `${hostUrl}/authorize`;
|
||||
|
||||
// 构建 SSO 参数
|
||||
const ssoParams = new URLSearchParams();
|
||||
|
@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import * as React from "react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Loader2, MessageCircleCode } from "lucide-react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
@ -18,26 +18,17 @@ export function UserAuthForm({
|
||||
}: React.HTMLAttributes<HTMLDivElement>) {
|
||||
const [isLoading, setIsLoading] = React.useState<boolean>(false);
|
||||
const { toast } = useToast();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const signIn = async () => {
|
||||
if (isLoading) return;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
// 构建请求体,包含 OAuth 参数
|
||||
const body: Record<string, any> = {};
|
||||
const currentParams = searchParams?.toString();
|
||||
if (currentParams) {
|
||||
body.oauth_params = currentParams;
|
||||
}
|
||||
|
||||
const response = await fetch("/api/auth/q58", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { signIn } from "@/actions/user-authorize";
|
||||
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
@ -18,7 +18,6 @@ export function UserAuthorize({
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [error, setError] = useState<Error | unknown>(null);
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const signInCallback = useCallback(async () => {
|
||||
if (isLoading) {
|
||||
@ -27,13 +26,12 @@ export function UserAuthorize({
|
||||
setIsLoading(true);
|
||||
try {
|
||||
// 从 URL 中获取 sso 和 sig 参数
|
||||
const sso = searchParams?.get("sso");
|
||||
const sig = searchParams?.get("sig");
|
||||
const url = new URL(window.location.href);
|
||||
const sso = url.searchParams.get("sso");
|
||||
const sig = url.searchParams.get("sig");
|
||||
|
||||
if (!sso || !sig) {
|
||||
// 如果没有 SSO 参数,说明是 OAuth 流程,跳转到登录页
|
||||
router.push(`/sign-in?${searchParams?.toString()}`);
|
||||
return;
|
||||
throw new Error("缺少必要的认证参数");
|
||||
}
|
||||
|
||||
// 传递 SSO 参数
|
||||
@ -41,12 +39,16 @@ export function UserAuthorize({
|
||||
sso,
|
||||
sig,
|
||||
});
|
||||
|
||||
// 登录成功后刷新路由状态
|
||||
router.refresh();
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.error("登录过程出错:", error);
|
||||
setError(error);
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [isLoading, router, searchParams]);
|
||||
}, [isLoading, router]);
|
||||
|
||||
useEffect(() => {
|
||||
signInCallback();
|
||||
|
Loading…
x
Reference in New Issue
Block a user