mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
feat: 改进 OAuth 参数存储和处理机制,优化跨页面参数传递
This commit is contained in:
parent
e193568ee8
commit
178d2af9aa
@ -18,13 +18,22 @@ export async function signIn(data: Record<string, any>) {
|
|||||||
redirect("/dashboard");
|
redirect("/dashboard");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否是 OAuth 流程
|
// 检查是否有保存的 OAuth 参数
|
||||||
|
const cookieStore = cookies();
|
||||||
|
const savedOAuthParams = cookieStore.get("oauth_params");
|
||||||
|
|
||||||
|
if (savedOAuthParams) {
|
||||||
|
// 清除保存的参数
|
||||||
|
cookieStore.delete("oauth_params");
|
||||||
|
// 重定向到 OAuth 授权页面
|
||||||
|
redirect(`/oauth/authorize?${savedOAuthParams.value}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有 OAuth 参数,尝试解析 return_sso_url
|
||||||
try {
|
try {
|
||||||
const returnUrl = new URL(returnSsoUrl);
|
const returnUrl = new URL(returnSsoUrl);
|
||||||
const isOAuthFlow = returnUrl.pathname.startsWith("/oauth/authorize");
|
if (returnUrl.pathname === "/authorize") {
|
||||||
|
// 如果是授权页面,直接重定向
|
||||||
if (isOAuthFlow) {
|
|
||||||
// 如果是 OAuth 流程,继续授权流程
|
|
||||||
redirect(returnSsoUrl);
|
redirect(returnSsoUrl);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -33,9 +33,12 @@ export async function POST(req: Request) {
|
|||||||
? `${hostUrl}/authorize?${oauthParams}` // OAuth流程:回到授权页面
|
? `${hostUrl}/authorize?${oauthParams}` // OAuth流程:回到授权页面
|
||||||
: `${hostUrl}/dashboard`; // 普通登录:直接到仪表板
|
: `${hostUrl}/dashboard`; // 普通登录:直接到仪表板
|
||||||
|
|
||||||
const sso = btoa(
|
// 构建 SSO 参数
|
||||||
`nonce=${nonce}&return_sso_url=${encodeURIComponent(return_url)}`,
|
const ssoParams = new URLSearchParams();
|
||||||
);
|
ssoParams.set("nonce", nonce);
|
||||||
|
ssoParams.set("return_sso_url", return_url);
|
||||||
|
|
||||||
|
const sso = btoa(ssoParams.toString());
|
||||||
const sig = hmacSHA256(sso, clientSecret).toString(Hex);
|
const sig = hmacSHA256(sso, clientSecret).toString(Hex);
|
||||||
|
|
||||||
cookies().set(AUTH_NONCE, nonce, { maxAge: 60 * 10 });
|
cookies().set(AUTH_NONCE, nonce, { maxAge: 60 * 10 });
|
||||||
|
@ -21,13 +21,21 @@ export function UserAuthForm({
|
|||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// 在组件挂载时保存 OAuth 参数
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (searchParams?.toString()) {
|
||||||
|
localStorage.setItem("oauth_params", searchParams.toString());
|
||||||
|
}
|
||||||
|
}, [searchParams]);
|
||||||
|
|
||||||
const signIn = () => {
|
const signIn = () => {
|
||||||
React.startTransition(async () => {
|
React.startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
// 构建请求体,包含 OAuth 参数
|
// 构建请求体,包含 OAuth 参数
|
||||||
const body: Record<string, any> = {};
|
const body: Record<string, any> = {};
|
||||||
if (searchParams?.toString()) {
|
const savedParams = localStorage.getItem("oauth_params");
|
||||||
body.oauth_params = searchParams.toString();
|
if (savedParams) {
|
||||||
|
body.oauth_params = savedParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch("/api/auth/q58", {
|
const response = await fetch("/api/auth/q58", {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user