-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
57 lines (40 loc) · 1.42 KB
/
proxy.ts
File metadata and controls
57 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
/**
* Proxy configuration for Next.js
* This handles API proxying to the backend FastAPI server
*/
const API_PREFIX = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8000';
// 不需要认证的路径
const publicPaths = ['/'];
export default async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
console.log('pathname', pathname);
// 检查是否有 access_token(从 cookie 中读取)
const token = request.cookies.get('access_token')?.value;
// 如果是公开路径,直接放行
if (publicPaths.some(path => pathname === path)) {
return NextResponse.next();
}
// 如果是受保护路径,检查是否已登录
if (!token) {
// 未登录,重定向到首页
const homeUrl = new URL('/', request.url);
homeUrl.searchParams.set('from', pathname);
return NextResponse.redirect(homeUrl);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* 匹配所有路径,除了:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
'/((?!api|_next/static|_next/image|favicon.ico|.*\\..*|public).*)',
],
};