Adicionar src/middleware.ts

This commit is contained in:
2024-11-27 13:19:08 -08:00
parent ff13cc27e5
commit 4661f8ab6f

26
src/middleware.ts Normal file
View File

@ -0,0 +1,26 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Verifica se é uma rota admin
if (request.nextUrl.pathname.startsWith('/admin')) {
// Excluir a página de login da verificação
if (request.nextUrl.pathname === '/admin/login') {
return NextResponse.next()
}
// Verificar se está autenticado
const isAuthenticated = request.cookies.get('auth_token')
if (!isAuthenticated) {
// Redirecionar para login
return NextResponse.redirect(new URL('/admin/login', request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: ['/admin/:path*']
}