feat: add authentication test page and API testing scripts; implement debug auth endpoint and enhance task route with read permissions
This commit is contained in:
@@ -10,20 +10,21 @@ const ROLE_HIERARCHY = {
|
||||
}
|
||||
|
||||
export function withAuth(handler, options = {}) {
|
||||
return async (req, context) => {
|
||||
return auth(async (req) => {
|
||||
try {
|
||||
const session = await auth(req)
|
||||
|
||||
// Check if user is authenticated
|
||||
if (!session?.user) {
|
||||
if (!req.auth?.user) {
|
||||
console.log("No session found for request to:", req.url)
|
||||
return NextResponse.json(
|
||||
{ error: "Authentication required" },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
console.log("Session found for user:", req.auth.user.email)
|
||||
|
||||
// Check role-based permissions (without database access)
|
||||
if (options.requiredRole && !hasPermission(session.user.role, options.requiredRole)) {
|
||||
if (options.requiredRole && !hasPermission(req.auth.user.role, options.requiredRole)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Insufficient permissions" },
|
||||
{ status: 403 }
|
||||
@@ -32,14 +33,14 @@ export function withAuth(handler, options = {}) {
|
||||
|
||||
// Add user info to request
|
||||
req.user = {
|
||||
id: session.user.id,
|
||||
email: session.user.email,
|
||||
name: session.user.name,
|
||||
role: session.user.role
|
||||
id: req.auth.user.id,
|
||||
email: req.auth.user.email,
|
||||
name: req.auth.user.name,
|
||||
role: req.auth.user.role
|
||||
}
|
||||
|
||||
// Call the original handler
|
||||
return await handler(req, context)
|
||||
return await handler(req)
|
||||
} catch (error) {
|
||||
console.error("Auth middleware error:", error)
|
||||
return NextResponse.json(
|
||||
@@ -47,7 +48,7 @@ export function withAuth(handler, options = {}) {
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function hasPermission(userRole, requiredRole) {
|
||||
@@ -64,12 +65,12 @@ export function withUserAuth(handler) {
|
||||
return withAuth(handler, { requiredRole: 'user' })
|
||||
}
|
||||
|
||||
// Helper for admin-level operations
|
||||
export function withAdminAuth(handler) {
|
||||
return withAuth(handler, { requiredRole: 'admin' })
|
||||
}
|
||||
|
||||
// Helper for project manager operations
|
||||
export function withManagerAuth(handler) {
|
||||
return withAuth(handler, { requiredRole: 'project_manager' })
|
||||
}
|
||||
|
||||
// Helper for admin operations
|
||||
export function withAdminAuth(handler) {
|
||||
return withAuth(handler, { requiredRole: 'admin' })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user