-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (38 loc) · 1.46 KB
/
middleware.ts
File metadata and controls
46 lines (38 loc) · 1.46 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const token = request.cookies.get('auth-token')?.value;
const pathname = request.nextUrl.pathname;
// Public routes that don't require authentication
const publicRoutes = ['/login', '/register', '/api/auth/login', '/api/auth/register'];
const isPublicRoute = publicRoutes.some(route => pathname.startsWith(route));
// Static assets and Next.js internals
if (
pathname.startsWith('/_next') ||
pathname.startsWith('/static') ||
pathname.includes('.') ||
pathname === '/favicon.ico'
) {
return NextResponse.next();
}
// Redirect to login if no token and not on public route
if (!token && !isPublicRoute) {
return NextResponse.redirect(new URL('/login', request.url));
}
// Redirect to dashboard if token exists and trying to access login/register
if (token && (pathname === '/login' || pathname === '/register')) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
// Redirect root to dashboard if authenticated, otherwise to login
if (pathname === '/') {
if (token) {
return NextResponse.redirect(new URL('/dashboard', request.url));
} else {
return NextResponse.redirect(new URL('/login', request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
};