Repository navigation
Expand file tree
/
Copy pathauthMiddleware1.js
More file actions
142 lines (120 loc) · 3.43 KB
/
Copy pathauthMiddleware1.js
File metadata and controls
142 lines (120 loc) · 3.43 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// middleware/authMiddleware.js
// const { PrismaClient } = require('@prisma/client');
// const prisma = new PrismaClient();
import { Prisma } from './src/config/db.js';
export const verifyAdmin = async (req, res, next) => {
try {
// Get token from Authorization header
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({
status: 'error',
message: 'Authentication token required. Format: Bearer <token>'
});
}
const token = authHeader.replace('Bearer ', '');
if (!token) {
return res.status(401).json({
status: 'error',
message: 'Token is missing'
});
}
// Find admin by token
const admin = await Prisma.ci_admin.findFirst({
where: {
token: token,
is_active: 1 // Only active admins
}
});
if (!admin) {
return res.status(401).json({
status: 'error',
message: 'Invalid or expired token. Please login again.'
});
}
// Attach admin to request object for use in routes
req.admin = {
admin_id: admin.admin_id,
username: admin.username,
email: admin.email,
firstname: admin.firstname,
lastname: admin.lastname,
admin_role_id: admin.admin_role_id,
is_supper: admin.is_supper
};
// Continue to the next middleware/route
next();
} catch (error) {
console.error('Auth middleware error:', error);
// Handle specific Prisma errors
if (error.name === 'PrismaClientKnownRequestError') {
return res.status(500).json({
status: 'error',
message: 'Database error during authentication'
});
}
return res.status(500).json({
status: 'error',
message: 'Authentication failed'
});
}
};
// Optional: Middleware to check admin role/permissions
export const checkPermission = (requiredRole = null) => {
return (req, res, next) => {
if (!req.admin) {
return res.status(401).json({
status: 'error',
message: 'Authentication required'
});
}
// Super admin has all permissions
if (req.admin.is_supper === 1) {
return next();
}
// Check specific role if required
if (requiredRole && req.admin.admin_role_id !== requiredRole) {
return res.status(403).json({
status: 'error',
message: 'Insufficient permissions'
});
}
next();
};
};
// Optional: Public routes that don't require authentication
export const optionalAuth = async (req, res, next) => {
try {
const authHeader = req.headers.authorization;
if (authHeader && authHeader.startsWith('Bearer ')) {
const token = authHeader.replace('Bearer ', '');
const admin = await Prisma.ci_admin.findFirst({
where: {
token: token,
is_active: 1
}
});
if (admin) {
req.admin = {
admin_id: admin.admin_id,
username: admin.username,
email: admin.email,
firstname: admin.firstname,
lastname: admin.lastname,
admin_role_id: admin.admin_role_id,
is_supper: admin.is_supper
};
}
}
next();
} catch (error) {
// Don't block request for optional auth
console.error('Optional auth error:', error);
next();
}
};
// module.exports = {
// verifyAdmin,
// checkPermission,
// optionalAuth
// };