mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-19 06:21:55 +08:00
refactor: Simplify SSO authentication route and OAuth parameter handling
This commit is contained in:
parent
d99b9bcc77
commit
d83f60b0a9
@ -9,35 +9,14 @@ const hostUrl = process.env.NEXT_PUBLIC_HOST_URL as string;
|
||||
const discourseHost = process.env.DISCOURSE_HOST as string;
|
||||
const clientSecret = process.env.DISCOURSE_SECRET as string;
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
export async function POST(_req: Request) {
|
||||
const nonce = WordArray.random(16).toString();
|
||||
let return_url = `${hostUrl}/dashboard`; // 默认重定向到仪表板
|
||||
|
||||
try {
|
||||
const body = await req.json();
|
||||
if (body.oauth_params) {
|
||||
return_url = `${hostUrl}/oauth/authorize?${body.oauth_params}`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to parse request body:", error);
|
||||
}
|
||||
|
||||
const return_url = `${hostUrl}/authorize`;
|
||||
const sso = btoa(`nonce=${nonce}&return_sso_url=${return_url}`);
|
||||
const sig = hmacSHA256(sso, clientSecret).toString(Hex);
|
||||
|
||||
cookies().set(AUTH_NONCE, nonce, {
|
||||
maxAge: 60 * 10,
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
});
|
||||
|
||||
cookies().set(AUTH_NONCE, nonce, { maxAge: 60 * 10 });
|
||||
return Response.json({
|
||||
sso_url: `${discourseHost}/session/sso_provider?sso=${sso}&sig=${sig}`,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("SSO 处理错误:", error);
|
||||
return Response.json({ error: "处理登录请求时发生错误" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
@ -24,11 +24,11 @@ export function UserAuthForm({
|
||||
const signIn = () => {
|
||||
React.startTransition(async () => {
|
||||
try {
|
||||
// 构建请求体,包含 OAuth 参数
|
||||
const body: Record<string, any> = {};
|
||||
const oauthParams = new URLSearchParams();
|
||||
|
||||
// 收集 OAuth 相关参数
|
||||
// 如果存在 OAuth 参数,添加到请求体
|
||||
if (searchParams?.has("client_id")) {
|
||||
const oauthParams = new URLSearchParams();
|
||||
[
|
||||
"client_id",
|
||||
"redirect_uri",
|
||||
@ -41,11 +41,10 @@ export function UserAuthForm({
|
||||
oauthParams.append(param, value);
|
||||
}
|
||||
});
|
||||
|
||||
// 如果存在 OAuth 参数,添加到请求体
|
||||
if (oauthParams.toString()) {
|
||||
body.oauth_params = oauthParams.toString();
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch("/api/auth/q58", {
|
||||
method: "POST",
|
||||
|
Loading…
x
Reference in New Issue
Block a user