feat: Add middleware for authentication and role-based access control
- Implemented a new middleware to handle authentication checks and enforce role-based access for protected routes. - Added debug logging to track token presence and user details for improved troubleshooting. - Configured middleware to match all request paths except for static files and specific assets.
This commit is contained in:
parent
f9bfa5febb
commit
395869c6c0
67
middleware.ts
Normal file
67
middleware.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import { NextResponse } from "next/server"
|
||||||
|
import type { NextRequest } from "next/server"
|
||||||
|
import { getToken } from "next-auth/jwt"
|
||||||
|
|
||||||
|
export async function middleware(request: NextRequest) {
|
||||||
|
const pathname = request.nextUrl.pathname
|
||||||
|
|
||||||
|
// Public routes - allow access
|
||||||
|
if (pathname === "/login" || pathname.startsWith("/api/auth")) {
|
||||||
|
return NextResponse.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get token (works in Edge runtime)
|
||||||
|
// getToken automatically detects the cookie name from NextAuth config
|
||||||
|
const token = await getToken({
|
||||||
|
req: request,
|
||||||
|
secret: process.env.NEXTAUTH_SECRET
|
||||||
|
})
|
||||||
|
|
||||||
|
// Debug logging for production troubleshooting
|
||||||
|
if (!token) {
|
||||||
|
console.log("Middleware: No token found", {
|
||||||
|
pathname,
|
||||||
|
cookieHeader: request.headers.get("cookie")?.substring(0, 200),
|
||||||
|
origin: request.headers.get("origin"),
|
||||||
|
referer: request.headers.get("referer")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
console.log("Middleware: Token found", {
|
||||||
|
pathname,
|
||||||
|
tokenId: token.id,
|
||||||
|
tokenRole: token.role,
|
||||||
|
tokenEmail: token.email
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protected routes - require authentication
|
||||||
|
if (!token) {
|
||||||
|
const loginUrl = new URL("/login", request.url)
|
||||||
|
loginUrl.searchParams.set("callbackUrl", pathname)
|
||||||
|
return NextResponse.redirect(loginUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Admin routes - require ADMIN role
|
||||||
|
if (pathname.startsWith("/admin")) {
|
||||||
|
if (token.role !== "ADMIN") {
|
||||||
|
return NextResponse.redirect(new URL("/", request.url))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: [
|
||||||
|
/*
|
||||||
|
* Match all request paths except for the ones starting with:
|
||||||
|
* - _next/static (static files)
|
||||||
|
* - _next/image (image optimization files)
|
||||||
|
* - _next/rsc (RSC payload requests)
|
||||||
|
* - _next/webpack (webpack chunks)
|
||||||
|
* - favicon.ico (favicon file)
|
||||||
|
* - public folder
|
||||||
|
*/
|
||||||
|
"/((?!_next/static|_next/image|_next/rsc|_next/webpack|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
|
||||||
|
],
|
||||||
|
}
|
||||||
12
proxy.ts
12
proxy.ts
@ -11,11 +11,23 @@ export async function proxy(request: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get token (works in Edge runtime)
|
// Get token (works in Edge runtime)
|
||||||
|
// getToken automatically detects the cookie name from NextAuth config
|
||||||
const token = await getToken({
|
const token = await getToken({
|
||||||
req: request,
|
req: request,
|
||||||
secret: process.env.NEXTAUTH_SECRET
|
secret: process.env.NEXTAUTH_SECRET
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Debug logging (remove in production if not needed)
|
||||||
|
if (process.env.NODE_ENV !== "production") {
|
||||||
|
console.log("Middleware token check:", {
|
||||||
|
pathname,
|
||||||
|
hasToken: !!token,
|
||||||
|
tokenId: token?.id,
|
||||||
|
tokenRole: token?.role,
|
||||||
|
cookieHeader: request.headers.get("cookie")?.substring(0, 100)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Protected routes - require authentication
|
// Protected routes - require authentication
|
||||||
if (!token) {
|
if (!token) {
|
||||||
const loginUrl = new URL("/login", request.url)
|
const loginUrl = new URL("/login", request.url)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user