35 lines
1016 B
TypeScript
35 lines
1016 B
TypeScript
import { NextResponse } from 'next/server'
|
|
import type { NextRequest } from 'next/server'
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const token = request.cookies.get('auth_token')
|
|
|
|
// Public paths that don't require authentication
|
|
if (
|
|
request.nextUrl.pathname.startsWith('/_next') ||
|
|
request.nextUrl.pathname.startsWith('/static') ||
|
|
request.nextUrl.pathname === '/login' ||
|
|
request.nextUrl.pathname === '/favicon.ico' ||
|
|
request.nextUrl.pathname.startsWith('/api/auth')
|
|
) {
|
|
// If user is already logged in and tries to access login, redirect to dashboard
|
|
if (token && request.nextUrl.pathname === '/login') {
|
|
return NextResponse.redirect(new URL('/', request.url))
|
|
}
|
|
return NextResponse.next()
|
|
}
|
|
|
|
// If no token, redirect to login
|
|
if (!token) {
|
|
return NextResponse.redirect(new URL('/login', request.url))
|
|
}
|
|
|
|
return NextResponse.next()
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
'/((?!_next/static|_next/image|favicon.ico).*)',
|
|
],
|
|
}
|