Adicionar src/middleware.ts
This commit is contained in:
26
src/middleware.ts
Normal file
26
src/middleware.ts
Normal 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*']
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user