feat: upgrade next-auth to v5.0.0-beta.29 and refactor authentication middleware

- Updated next-auth dependency in package.json to version 5.0.0-beta.29.
- Refactored create-admin script to use a valid email format.
- Implemented authentication middleware for various API routes to enforce access control.
- Refactored API route handlers to improve readability and maintainability.
- Enhanced error handling in authentication error page.
- Added detailed tests for authentication flow, including protected routes and NextAuth endpoints.
This commit is contained in:
2025-06-25 12:32:13 +02:00
parent 035a0386d7
commit c1bb4c44fd
24 changed files with 626 additions and 369 deletions

View File

@@ -1,6 +1,5 @@
import { getToken } from "next-auth/jwt"
import { auth } from "@/lib/auth"
import { NextResponse } from "next/server"
import db from "../db.js"
// Role hierarchy for permission checking
const ROLE_HIERARCHY = {
@@ -13,51 +12,30 @@ const ROLE_HIERARCHY = {
export function withAuth(handler, options = {}) {
return async (req, context) => {
try {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET })
const session = await auth(req)
// Check if user is authenticated
if (!token?.userId) {
if (!session?.user) {
return NextResponse.json(
{ error: "Authentication required" },
{ status: 401 }
)
}
// Check if user account is active
const user = db.prepare("SELECT is_active FROM users WHERE id = ?").get(token.userId)
if (!user?.is_active) {
return NextResponse.json(
{ error: "Account deactivated" },
{ status: 403 }
)
}
// Check role-based permissions
if (options.requiredRole && !hasPermission(token.role, options.requiredRole)) {
logAuditEvent(token.userId, 'ACCESS_DENIED', options.resource || 'api', req.url)
// Check role-based permissions (without database access)
if (options.requiredRole && !hasPermission(session.user.role, options.requiredRole)) {
return NextResponse.json(
{ error: "Insufficient permissions" },
{ status: 403 }
)
}
// Check resource-specific permissions
if (options.checkResourceAccess) {
const hasAccess = await options.checkResourceAccess(token, context.params)
if (!hasAccess) {
return NextResponse.json(
{ error: "Access denied to this resource" },
{ status: 403 }
)
}
}
// Add user info to request
req.user = {
id: token.userId,
email: token.email,
name: token.name,
role: token.role
id: session.user.id,
email: session.user.email,
name: session.user.name,
role: session.user.role
}
// Call the original handler
@@ -95,22 +73,3 @@ export function withManagerAuth(handler) {
export function withAdminAuth(handler) {
return withAuth(handler, { requiredRole: 'admin' })
}
// Audit logging helper
function logAuditEvent(userId, action, resourceType, resourceId, req = null) {
try {
db.prepare(`
INSERT INTO audit_logs (user_id, action, resource_type, resource_id, ip_address, user_agent)
VALUES (?, ?, ?, ?, ?, ?)
`).run(
userId,
action,
resourceType,
resourceId,
req?.ip || req?.socket?.remoteAddress || 'unknown',
req?.headers?.['user-agent'] || 'unknown'
)
} catch (error) {
console.error("Audit log error:", error)
}
}