forked from hagopj13/node-express-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
const roles = ['user', 'admin']; | ||
|
||
const roleRights = new Map(); | ||
roleRights.set(roles[0], []); | ||
roleRights.set(roles[1], ['getUsers', 'manageUsers']); | ||
|
||
module.exports = { | ||
roles, | ||
roleRights, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const passport = require('passport'); | ||
const httpStatus = require('http-status'); | ||
const AppError = require('../utils/AppError'); | ||
const { roleRights } = require('../config/roles'); | ||
|
||
const verifyCallback = (req, resolve, reject, requiredRights) => async (err, user, info) => { | ||
if (err || info || !user) { | ||
return reject(new AppError(httpStatus.UNAUTHORIZED, 'Please authenticate')); | ||
} | ||
req.user = user; | ||
|
||
if (requiredRights.length) { | ||
const userRights = roleRights.get(user.role); | ||
const hasRequiredRights = requiredRights.every(requiredRight => userRights.includes(requiredRight)); | ||
if (!hasRequiredRights && req.params.userId !== user.id) { | ||
return reject(new AppError(httpStatus.FORBIDDEN, 'Forbidden')); | ||
} | ||
} | ||
|
||
resolve(); | ||
}; | ||
|
||
const auth = (...requiredRights) => async (req, res, next) => { | ||
return new Promise((resolve, reject) => { | ||
passport.authenticate('jwt', { session: false }, verifyCallback(req, resolve, reject, requiredRights))(req, res, next); | ||
}) | ||
.then(() => next()) | ||
.catch(err => next(err)); | ||
}; | ||
|
||
module.exports = auth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
const express = require('express'); | ||
const auth = require('../../middlewares/auth'); | ||
const validate = require('../../middlewares/validate'); | ||
const userValidation = require('../../validations/user.validation'); | ||
const userController = require('../../controllers/user.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.route('/').post(validate(userValidation.createUser), userController.createUser); | ||
router.route('/').post(auth('manageUsers'), validate(userValidation.createUser), userController.createUser); | ||
|
||
module.exports = router; |