mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
feat: 改进 OAuth 参数存储和重定向机制,使用 cookies 管理临时授权参数
This commit is contained in:
parent
8bcaf1fd89
commit
05b8ee3c75
@ -1,5 +1,6 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
|
import { cookies } from "next/headers";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { signIn as nextSignIn } from "@/auth";
|
import { signIn as nextSignIn } from "@/auth";
|
||||||
|
|
||||||
@ -10,16 +11,19 @@ export async function signIn(data: Record<string, any>) {
|
|||||||
// 进行 SSO 登录
|
// 进行 SSO 登录
|
||||||
await nextSignIn("credentials", { sso, sig });
|
await nextSignIn("credentials", { sso, sig });
|
||||||
|
|
||||||
// 从 sso 参数中获取 return_sso_url
|
// 检查是否有保存的 OAuth 参数
|
||||||
const params = new URLSearchParams(atob(sso));
|
const cookieStore = cookies();
|
||||||
const returnSsoUrl = params.get("return_sso_url");
|
const oauthParams = cookieStore.get("oauth_params");
|
||||||
|
|
||||||
if (!returnSsoUrl) {
|
if (oauthParams) {
|
||||||
redirect("/dashboard");
|
// 清除 cookie
|
||||||
|
cookieStore.delete("oauth_params");
|
||||||
|
// 重定向到授权页面,带上 OAuth 参数
|
||||||
|
redirect(`/oauth/authorize?${oauthParams.value}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重定向到 return_sso_url
|
// 如果没有 OAuth 参数,重定向到仪表板
|
||||||
redirect(returnSsoUrl);
|
redirect("/dashboard");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("登录失败:", error);
|
console.error("登录失败:", error);
|
||||||
redirect("/sign-in?error=AuthenticationError");
|
redirect("/sign-in?error=AuthenticationError");
|
||||||
|
@ -13,8 +13,20 @@ export async function POST(req: Request) {
|
|||||||
try {
|
try {
|
||||||
const nonce = WordArray.random(16).toString();
|
const nonce = WordArray.random(16).toString();
|
||||||
|
|
||||||
// 设置基本的回调地址
|
// 从请求中获取 OAuth 参数
|
||||||
|
const body = await req.json().catch(() => ({}));
|
||||||
|
const oauthParams = body.oauth_params || "";
|
||||||
|
|
||||||
|
// 设置回调地址,如果有 OAuth 参数则保存
|
||||||
const return_url = `${hostUrl}/authorize`;
|
const return_url = `${hostUrl}/authorize`;
|
||||||
|
if (oauthParams) {
|
||||||
|
cookies().set("oauth_params", oauthParams, {
|
||||||
|
maxAge: 60 * 10, // 10分钟过期
|
||||||
|
path: "/",
|
||||||
|
httpOnly: true,
|
||||||
|
secure: process.env.NODE_ENV === "production",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 构建 SSO 参数
|
// 构建 SSO 参数
|
||||||
const ssoParams = new URLSearchParams();
|
const ssoParams = new URLSearchParams();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { signIn } from "@/actions/user-authorize";
|
import { signIn } from "@/actions/user-authorize";
|
||||||
|
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
@ -18,6 +18,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 router = useRouter();
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const signInCallback = useCallback(async () => {
|
const signInCallback = useCallback(async () => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@ -26,9 +27,8 @@ export function UserAuthorize({
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
// 从 URL 中获取 sso 和 sig 参数
|
// 从 URL 中获取 sso 和 sig 参数
|
||||||
const url = new URL(window.location.href);
|
const sso = searchParams?.get("sso");
|
||||||
const sso = url.searchParams.get("sso");
|
const sig = searchParams?.get("sig");
|
||||||
const sig = url.searchParams.get("sig");
|
|
||||||
|
|
||||||
if (!sso || !sig) {
|
if (!sso || !sig) {
|
||||||
throw new Error("缺少必要的认证参数");
|
throw new Error("缺少必要的认证参数");
|
||||||
@ -48,7 +48,7 @@ export function UserAuthorize({
|
|||||||
setError(error);
|
setError(error);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [isLoading, router]);
|
}, [isLoading, router, searchParams]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
signInCallback();
|
signInCallback();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user