mirror of
https://github.com/woodchen-ink/Q58Connect.git
synced 2025-07-18 14:01:55 +08:00
feat: Improve OAuth authorization flow and redirect handling
- Update signIn action to support dynamic redirect after authentication - Modify Q58 OAuth route to handle dynamic return URL with original parameters - Refactor UserAuthorize component to support OAuth-specific redirect scenarios - Add support for preserving and processing OAuth parameters during login
This commit is contained in:
parent
f7d32436b5
commit
1e519738ea
@ -1,7 +1,10 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
import { signIn as nextSignIn } from "@/auth";
|
import { signIn as nextSignIn } from "@/auth";
|
||||||
|
|
||||||
export async function signIn(data: Record<string, any>) {
|
export async function signIn(data: Record<string, any>) {
|
||||||
return nextSignIn("credentials", data);
|
const { redirectTo, ...credentials } = data;
|
||||||
|
await nextSignIn("credentials", credentials);
|
||||||
|
redirect(redirectTo || "/dashboard");
|
||||||
}
|
}
|
||||||
|
@ -9,10 +9,12 @@ const hostUrl = process.env.NEXT_PUBLIC_HOST_URL as string;
|
|||||||
const discourseHost = process.env.DISCOURSE_HOST as string;
|
const discourseHost = process.env.DISCOURSE_HOST as string;
|
||||||
const clientSecret = process.env.DISCOURSE_SECRET as string;
|
const clientSecret = process.env.DISCOURSE_SECRET as string;
|
||||||
|
|
||||||
export async function POST(_req: Request) {
|
export async function POST(req: Request) {
|
||||||
const nonce = WordArray.random(16).toString();
|
const nonce = WordArray.random(16).toString();
|
||||||
const return_url = `${hostUrl}/authorize`;
|
const url = new URL(req.url);
|
||||||
const sso = btoa(`nonce=${nonce}&return_sso_url=${return_url}`);
|
const originalParams = url.searchParams.toString();
|
||||||
|
const return_url = `${hostUrl}/q58/callback?${originalParams}`;
|
||||||
|
const sso = btoa(`nonce=${nonce}&return_sso_url=${encodeURI(return_url)}`);
|
||||||
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 });
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { signIn } from "@/actions/user-authorize";
|
||||||
import { signIn as nextAuthSignIn, useSession } from "next-auth/react";
|
|
||||||
|
|
||||||
interface UserAuthorizeProps extends React.HTMLAttributes<HTMLDivElement> {
|
interface UserAuthorizeProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||||
data: Record<string, any>;
|
data: Record<string, any>;
|
||||||
@ -15,8 +14,6 @@ export function UserAuthorize({
|
|||||||
}: UserAuthorizeProps) {
|
}: UserAuthorizeProps) {
|
||||||
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 { update } = useSession();
|
|
||||||
const router = useRouter();
|
|
||||||
|
|
||||||
const signInCallback = useCallback(async () => {
|
const signInCallback = useCallback(async () => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@ -24,28 +21,32 @@ export function UserAuthorize({
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await nextAuthSignIn("credentials", {
|
// 检查是否有 OAuth 参数
|
||||||
...data,
|
const searchParams = new URLSearchParams(data);
|
||||||
redirect: false,
|
const sso = searchParams.get("sso");
|
||||||
});
|
const sig = searchParams.get("sig");
|
||||||
|
const oauth = searchParams.get("oauth");
|
||||||
|
|
||||||
if (result?.error) {
|
let redirectTo = "/dashboard";
|
||||||
setError(result.error);
|
if (sso && sig && oauth) {
|
||||||
} else {
|
// 如果是从Q58论坛回调,使用原始的OAuth参数
|
||||||
// 更新 session
|
const oauthParams = new URLSearchParams(atob(oauth));
|
||||||
await update();
|
redirectTo = `/oauth/authorize?${oauthParams.toString()}`;
|
||||||
// 重定向回OAuth授权页面
|
|
||||||
router.push(`/oauth/authorize${window.location.search}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await signIn({ ...data, redirectTo });
|
||||||
|
setIsLoading(false);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(error);
|
setError(error);
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}, [data, isLoading, update, router]);
|
}, [data, isLoading]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
signInCallback();
|
const timer = setTimeout(signInCallback, 5);
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timer);
|
||||||
|
};
|
||||||
}, [signInCallback]);
|
}, [signInCallback]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -53,7 +54,7 @@ export function UserAuthorize({
|
|||||||
{error ? (
|
{error ? (
|
||||||
<p className="text-center">登录异常,授权失败!</p>
|
<p className="text-center">登录异常,授权失败!</p>
|
||||||
) : (
|
) : (
|
||||||
<p className="text-center">账号信息验证中,请稍等...</p>
|
<p className="text-center">账号信息验证中,准备跳转中,请稍等...</p>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user