Skip to content

Instantly share code, notes, and snippets.

@fcingolani
Last active August 29, 2015 14:21
Show Gist options
  • Save fcingolani/92e0502ae77df82d795e to your computer and use it in GitHub Desktop.
Save fcingolani/92e0502ae77df82d795e to your computer and use it in GitHub Desktop.
Ejemplo de authn simple en ExpressJS
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