node.js - HTTP Auth with dynamic URLs in Express.JS -
i've got route in express 3.0 app defined this:
app.post('/:app/find', auth, function(req, res){
i'd restricted using http authorization. auth
function looks this:
var auth = express.basicauth(function(user, pass, callback){ callback(null, user === 'user' && pass === 'pass'); });
i'd auth
function database lookup based on req.params.app
url, , authenticate users accordingly. problem i'm not sure how access yet, since function defined in app.post
hasn't been called yet when auth
run. can it? there different implementation of http auth should using?
there isn't proper way of doing express.basicauth
(i looked @ other modules provide similar functionality, never seem pass req
authentication function either), create middleware instantiates every request want authenticate:
var express = require('express'); var app = express(); var basicauth = express.basicauth; var auth = function(req, res, next) { ...here can access 'req'... basicauth(function(user, pass, callback) { ...here can use variables set in block above... callback(null, user === 'user' && pass === 'pass'); })(req, res, next); }; app.get('/', auth, function(req, res) { res.send('authenticated!'); }); app.listen(3012);
Comments
Post a Comment