I am having some trouble creating a middleware that will check with my auth service to see whether a user is valid or not and I also want to check for their role being a certain thing. I'm unsure how to properly pass in the options to request. Here is an example of what I want my middleware to look likeexports.isAuthenticated = (req, res, next, options, role) => {options.headers.authorization = req.headers.authorization;request.get(req.path, options, (error, response, body) => {if (error) res.status(500);if(body.role == role) {next();} else {res.status(403).send("Forbidden");}});};I also tried this which is a slight modification of what I found online but it does not take in the role param that I want to be able to. I know this one does not check with my auth service but it seemed like the best thing I could find at the time and the above is my take on it while trying to do it myself.exports.isAuthenticated = (req, res, next) => {if (!req.headers['authorization']) {res.status(401).send("Unauthorized");} else {jwt.verify(req.headers['authorization'], config.passport.jwtSecret, (err, decoded) => {if (err) {console.log(err);res.status(403).send("Forbidden");} else {console.log(decoded);next();}});}};This is how I call it in my routes fileconst isAuthenticated = require('../middleware/authorization').isAuthenticated;This is my ideal routerouter.get('/foo',isAuthenticated(admin), // Where 'admin' is my role I want to check forsomeController,(req, res) => {console.log('I work!')});I'm not sure if this is even possible or how to do it, I look forward to suggestions on how to do this. Thanks in advance.
Submitted February 07, 2019 at 02:11AM by jsdfkljdsafdsu980p
No comments:
Post a Comment