Last active
August 29, 2015 14:21
-
-
Save fcingolani/92e0502ae77df82d795e to your computer and use it in GitHub Desktop.
Ejemplo de authn simple en ExpressJS
This file contains hidden or 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
var express = require('express'); | |
var app = express(); | |
// middleware que obtiene los roles del usuario actual | |
app.use(function (req, res, next){ | |
if(req.query.admin){ | |
req.role = "admin"; | |
}else{ | |
req.role = "anon"; | |
}; | |
next(); | |
}); | |
function restrict (roles){ | |
return function (req, res, next){ | |
if(roles.indexOf(req.role) === -1 ){ | |
res.sendStatus(403); | |
}else{ | |
next(); | |
} | |
}; | |
}; | |
app.get('/', restrict(['anon', 'admin']), function (req, res) { | |
res.send('Hola'); | |
}); | |
app.get('/secreto', restrict(['admin']), function (req, res) { | |
res.send('Mundo'); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment