-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
41 lines (33 loc) · 1.07 KB
/
middleware.js
File metadata and controls
41 lines (33 loc) · 1.07 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
import { getToken } from "next-auth/jwt"
import { NextResponse } from "next/server"
export async function middleware(request) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET
})
const isAuthPage = request.nextUrl.pathname.startsWith('/auth')
const isAdminPage = request.nextUrl.pathname.startsWith('/admin')
// Se não estiver autenticado e não estiver na página de auth
if (!token && !isAuthPage) {
return NextResponse.redirect(new URL('/auth/login', request.url))
}
// Se estiver autenticado e tentar acessar página de auth
if (token && isAuthPage) {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
// Verificação específica para área admin
if (isAdminPage) {
if (!token || token.role !== 'ADMIN') {
console.log('Usuário não é admin:') // Para debug
return NextResponse.redirect(new URL('/dashboard', request.url))
}
}
return NextResponse.next()
}
export const config = {
matcher: [
'/dashboard/:path*',
'/admin/:path*',
'/auth/:path*'
]
}