Skip to content

Commit 6f40726

Browse files
committed
fix: 回调地址
1 parent ed1d420 commit 6f40726

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

app/api/upload/route.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ export async function POST(request: NextRequest) {
4242
return NextResponse.json({ error: "未授权访问" }, { status: 401 });
4343
}
4444

45-
// 调用后端 /api/v1/auth/me 验证 token 并获取用户信息
46-
const meRes = await fetch("http://localhost:8080/api/v1/auth/me", {
45+
// 调用后端 /auth/me 验证 token(服务端直连后端,走 BACKEND_URL 环境变量)
46+
const backendUrl = process.env.BACKEND_URL ?? "http://localhost:8080";
47+
const meRes = await fetch(`${backendUrl}/auth/me`, {
4748
headers: { satoken: token },
4849
});
4950
if (!meRes.ok) {

app/components/SignInButton.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ interface SignInButtonProps {
77
}
88

99
export function SignInButton({ className }: SignInButtonProps) {
10-
// 跳转到后端 GitHub OAuth 入口,后端完成授权后会带着 token 重定向回前端首页
10+
// 直接跳转到后端 GitHub OAuth 授权入口(NEXT_PUBLIC_BACKEND_URL)
11+
// 后端完成授权后带着 token 重定向回前端首页 /?token=xxx
1112
const handleSignIn = () => {
12-
window.location.href = "/api/v1/oauth/render/github";
13+
const backendUrl =
14+
process.env.NEXT_PUBLIC_BACKEND_URL ?? "http://localhost:8080";
15+
window.location.href = `${backendUrl}/oauth/render/github`;
1316
};
1417

1518
return (

lib/use-auth.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ function getStoredToken(): string | null {
3333
return localStorage.getItem("satoken");
3434
}
3535

36-
// 调用后端 /api/v1/auth/me 验证 token 并获取用户信息
36+
// 后端地址(浏览器直接访问,Next.js public env var)
37+
const BACKEND_URL =
38+
process.env.NEXT_PUBLIC_BACKEND_URL ?? "http://localhost:8080";
39+
40+
// 调用后端 /auth/me 验证 token 并获取用户信息
3741
async function fetchCurrentUser(token: string): Promise<UserView | null> {
3842
try {
39-
const res = await fetch("/api/v1/auth/me", {
43+
const res = await fetch(`${BACKEND_URL}/auth/me`, {
4044
headers: { satoken: token },
4145
});
4246
if (!res.ok) return null;
@@ -94,7 +98,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
9498
const token = getStoredToken();
9599
if (token) {
96100
try {
97-
await fetch("/api/v1/auth/logout", {
101+
await fetch(`${BACKEND_URL}/auth/logout`, {
98102
method: "POST",
99103
headers: { satoken: token },
100104
});

next.config.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ const withNextIntl = createNextIntlPlugin("./i18n.ts");
2222
const config = {
2323
reactStrictMode: true,
2424
async rewrites() {
25-
// 将 /api/v1/* 请求转发到本地 Spring Boot 后端(端口 8080)
26-
// 好处:前端和后端同源,无需处理 CORS,开发体验更好
25+
const backendUrl = process.env.BACKEND_URL ?? "http://localhost:8080";
2726
return [
2827
{
29-
source: "/api/v1/:path*",
30-
destination: "http://localhost:8080/api/v1/:path*",
28+
// GitHub OAuth 回调:GitHub 重定向到 Next.js,Next.js 再转发给后端
29+
// 路径与 GitHub OAuth App 注册的 callback URL 保持一致,无需改 GitHub App 设置
30+
source: "/api/auth/callback/github",
31+
destination: `${backendUrl}/api/auth/callback/github`,
3132
},
3233
];
3334
},

0 commit comments

Comments
 (0)