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");
|
||||
}
|
||||
|
||||
// 检查是否是 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 {
|
||||
const returnUrl = new URL(returnSsoUrl);
|
||||
const isOAuthFlow = returnUrl.pathname.startsWith("/oauth/authorize");
|
||||
|
||||
if (isOAuthFlow) {
|
||||
// 如果是 OAuth 流程,继续授权流程
|
||||
if (returnUrl.pathname === "/authorize") {
|
||||
// 如果是授权页面,直接重定向
|
||||
redirect(returnSsoUrl);
|
||||
}
|
||||
} catch (error) {
|
||||
|
@ -33,9 +33,12 @@ export async function POST(req: Request) {
|
||||
? `${hostUrl}/authorize?${oauthParams}` // OAuth流程:回到授权页面
|
||||
: `${hostUrl}/dashboard`; // 普通登录:直接到仪表板
|
||||
|
||||
const sso = btoa(
|
||||
`nonce=${nonce}&return_sso_url=${encodeURIComponent(return_url)}`,
|
||||
);
|
||||
// 构建 SSO 参数
|
||||
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);
|
||||
|
||||
cookies().set(AUTH_NONCE, nonce, { maxAge: 60 * 10 });
|
||||
|
@ -21,13 +21,21 @@ export function UserAuthForm({
|
||||
const { toast } = useToast();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
// 在组件挂载时保存 OAuth 参数
|
||||
React.useEffect(() => {
|
||||
if (searchParams?.toString()) {
|
||||
localStorage.setItem("oauth_params", searchParams.toString());
|
||||
}
|
||||
}, [searchParams]);
|
||||
|
||||
const signIn = () => {
|
||||
React.startTransition(async () => {
|
||||
try {
|
||||
// 构建请求体,包含 OAuth 参数
|
||||
const body: Record<string, any> = {};
|
||||
if (searchParams?.toString()) {
|
||||
body.oauth_params = searchParams.toString();
|
||||
const savedParams = localStorage.getItem("oauth_params");
|
||||
if (savedParams) {
|
||||
body.oauth_params = savedParams;
|
||||
}
|
||||
|
||||
const response = await fetch("/api/auth/q58", {
|
||||
|
Loading…
x
Reference in New Issue
Block a user