forked from realskyrin/plan-kanban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
41 lines (33 loc) · 1.06 KB
/
middleware.ts
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// 不需要认证的路由
const publicRoutes = ['/login', '/register']
// API 路由
const authApiRoutes = ['/api/auth/login', '/api/auth/register', '/api/auth/me']
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// 允许公开路由和认证 API 路由
if (publicRoutes.includes(pathname) || authApiRoutes.includes(pathname)) {
return NextResponse.next()
}
// 检查认证状态
const authToken = request.cookies.get('auth_token')
// 如果未认证,重定向到登录页面
if (!authToken) {
const loginUrl = new URL('/login', request.url)
loginUrl.searchParams.set('from', pathname)
return NextResponse.redirect(loginUrl)
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* 匹配所有路由,除了:
* - _next (Next.js 系统文件)
* - static (静态文件)
* - favicon.ico (浏览器图标)
*/
'/((?!_next/|static/|favicon.ico).*)',
],
}