@misalon/common 1.0.13 → 1.0.16
Sign up to get free protection for your applications and to get access to all the features.
package/build/index.d.ts
CHANGED
@@ -8,7 +8,6 @@ export * from './middlewares/current-user';
|
|
8
8
|
export * from './middlewares/error-handler';
|
9
9
|
export * from './middlewares/require-auth';
|
10
10
|
export * from './middlewares/validate-request';
|
11
|
-
export * from './middlewares/admin-auth';
|
12
11
|
export * from './middlewares/restrict-by-role';
|
13
12
|
export * from './events/base-listener';
|
14
13
|
export * from './events/base-publisher';
|
package/build/index.js
CHANGED
@@ -24,7 +24,6 @@ __exportStar(require("./middlewares/current-user"), exports);
|
|
24
24
|
__exportStar(require("./middlewares/error-handler"), exports);
|
25
25
|
__exportStar(require("./middlewares/require-auth"), exports);
|
26
26
|
__exportStar(require("./middlewares/validate-request"), exports);
|
27
|
-
__exportStar(require("./middlewares/admin-auth"), exports);
|
28
27
|
__exportStar(require("./middlewares/restrict-by-role"), exports);
|
29
28
|
__exportStar(require("./events/base-listener"), exports);
|
30
29
|
__exportStar(require("./events/base-publisher"), exports);
|
@@ -6,15 +6,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.currentUser = void 0;
|
7
7
|
var jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
8
8
|
var currentUser = function (req, res, next) {
|
9
|
-
var _a;
|
10
|
-
|
11
|
-
|
9
|
+
var _a, _b;
|
10
|
+
console.log('Session JWT:', (_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt); // Log the JWT from session
|
11
|
+
if (!((_b = req.session) === null || _b === void 0 ? void 0 : _b.jwt)) {
|
12
|
+
console.log('No JWT in session, skipping user authentication');
|
13
|
+
return next(); // No JWT in session, move to the next middleware
|
12
14
|
}
|
13
15
|
try {
|
16
|
+
// Log before decoding
|
17
|
+
console.log('Attempting to decode JWT...');
|
14
18
|
var payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY);
|
15
|
-
|
19
|
+
console.log('Decoded JWT Payload:', payload); // Log the decoded payload
|
20
|
+
req.currentUser = payload; // Attach user information to request
|
16
21
|
}
|
17
|
-
catch (err) {
|
18
|
-
|
22
|
+
catch (err) {
|
23
|
+
console.error('Error decoding JWT:', err); // Log any decoding errors
|
24
|
+
}
|
25
|
+
next(); // Proceed to the next middleware or route handler
|
19
26
|
};
|
20
27
|
exports.currentUser = currentUser;
|
@@ -5,6 +5,11 @@ exports.restrictByRole = void 0;
|
|
5
5
|
var not_authorized_error_1 = require("../errors/not-authorized-error"); // Adjust path as needed
|
6
6
|
var restrictByRole = function (restrictedRoles) {
|
7
7
|
return function (req, res, next) {
|
8
|
+
if (!req.currentUser) {
|
9
|
+
// If user is not authenticated, return a 401 error
|
10
|
+
res.status(401).send({ error: 'User not authenticated' });
|
11
|
+
return;
|
12
|
+
}
|
8
13
|
if (restrictedRoles.includes(req.currentUser.role)) {
|
9
14
|
throw new not_authorized_error_1.NotAuthorizedError();
|
10
15
|
}
|