Express passport-ldap nodejs如何将变量传递给另一个中间件

时间:2018-04-24 10:17:31

标签: express passport.js

嗨,请你能帮助我,

app.js上的

我使用passport-ldapauth startegy并且有效!

...

var OPTS = {
        server: {
            url: 'ldap://myipldap:389',
            bindDn: 'uid={{username}},O=My Company Group',
            //bindCredentials: '{{req.body.password}}',
            searchBase: 'O=My Company Group',
            searchFilter: 'uid={{username}}'                                                
        }
    };            
        passport.use(new LdapStrategy(OPTS));       
        passport.serializeUser(function(user, done) {
            done(null, user);
         });         
         passport.deserializeUser(function(user, done) {
            done(null, user);
         });

        app.post('/login', (req,res,next) => { 

            passport.authenticate('ldapauth', {
                //session: false,
                successRedirect:'/index',
                failureRedirect:'/login',
                failureFlash:true           
            })(req, res, next);
        });

....在执行成功重定向之前我需要查询表mysql并检查用户的授权

我想我这样做,我有成功重定向到索引,我有一个导出模块调用accessoSicuro:

// accessSicuro It's exported module:
module.exports = {
accessoSicuro:function(req,res,next){    
if(req.isAuthenticated()){    

/*
HERE I WANT to check the mysql table and pass the returned parameter to next () method and then use the value to the middleware index
*/
return next();
}
req.flash('msg_errore','Sorry, no entry, no auth ldap');
res.redirect('/login');
}
}   

app.get('/index',accessoSicuro,(req,res,next)=>{    

// HERE THE RESPONSE VALUE OF mysql response FROM accessoSicuro method

        sess=req.session;
        idUser = sess.passport.user;    
        res.render('index');    
    }); 

非常感谢

1 个答案:

答案 0 :(得分:0)

我相信你无法通过next()中间件传递变量。最佳实践(因为它是明文档中提到的那个)是使用res.locals