-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
63 lines (55 loc) · 1.77 KB
/
Copy pathmiddleware.ts
File metadata and controls
63 lines (55 loc) · 1.77 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { NextResponse } from 'next/server'
import { NextRequest } from 'next/server'
import { jwtVerify } from 'jose'
const AUTH_ROUTES = ['/auth/login', '/auth/register'] as const
const JWT_SECRET = process.env.NEXT_JWT_SECRET as string
const DASHBOARD_PATH = '/dashboard'
const LOGIN_PATH = '/auth/login'
const DASHBOARD_REDIRECT = '/dashboard/perfil'
async function verifyAndHandleToken(token: string) {
try {
await jwtVerify(token, new TextEncoder().encode(JWT_SECRET))
return true
} catch {
return false
}
}
/**
* Middleware principal de autenticación
*/
export async function middleware(request: NextRequest) {
const token = request.cookies.get('cookie-token')
const currentPath = request.nextUrl.pathname
// Manejo de rutas de autenticación
if (AUTH_ROUTES.includes(currentPath as any)) {
if (token) {
const isValid = await verifyAndHandleToken(token.value)
if (isValid) {
return NextResponse.redirect(new URL(DASHBOARD_REDIRECT, request.url))
}
// Si el token no es válido, lo eliminamos
const response = NextResponse.next()
response.cookies.delete('cookie-token')
return response
}
return NextResponse.next()
}
// Protección de rutas del dashboard
if (currentPath.startsWith(DASHBOARD_PATH)) {
if (!token) {
return NextResponse.redirect(new URL(LOGIN_PATH, request.url))
}
const isValid = await verifyAndHandleToken(token.value)
if (!isValid) {
const response = NextResponse.redirect(new URL(LOGIN_PATH, request.url))
response.cookies.delete('cookie-token')
return response
}
return NextResponse.next()
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*', '/auth/login', '/auth/register']
}
// MATCHER ->