将使用 'passport' 创建的 javascript var 传输到 express 路由器

transfer javascript var created with 'passport' to express router

我创建了这个 passportAuth.js 文件用于身份验证:

module.exports = function(passport, FacebookStrategy, config, mongoose, fbgraph){

passport.serializeUser(function(user,done){ //make the user reference available threw multiple pages
    done(null,user.id);
});

passport.deserializeUser(function(id,done){
    userModel.findById(id, function(err, user){
        done(err, user);
    })
})

passport.use(new FacebookStrategy({
    clientID: config.fb.appID,
    clientSecret: config.fb.appSecret,
    callbackURL: config.fb.callbackURL,
    profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(accessToken, refershToken, profile, done){

    var fb = new fbgraph.Facebook(accessToken, 'v2.2');
    fb.me(function(err, me) {
        console.log(me);
    });
    fb.my.events(function(err, events) {
        console.log(events);
    });
    fb.my.friends(function(err, result){
        console.log(result);
    });

}))

}

直到这里一切正常,我能够用我想要的数据创建 fb var。当我试图将此 fb 对象传输到我的 routes.js 文件中的快速路由器时,问题就开始了。我想将此对象中存储的数据渲染到 welcome.html 页面,但我不知道如何将此 var 传递给路由文件并将其传递给渲染函数。

这是我的路由文件的样子:

module.exports = function(express, app, passport){
    var router = express.Router();
    router.get('/', function(req,res,next){
        res.render('index', {title: "welcome to aDating"});
    })

    function securePages(req, res, next){
        if(req.isAuthenticated()){
            next();
        } else {
            res.redirect('/');
        }
    }

    router.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email', 'user_friends', 'public_profile', 'user_events' ] }));
        router.get('/auth/facebook/callback', passport.authenticate('facebook', {
            successRedirect:'/welcome', //if authentication is successful, navigate to this path
            failureRedirect:'/'         //else, navigate here
        }))

    router.get('/welcome', securePages, function(req, res, next){
        res.render('welcome', {title:'Welcome to aDating', user:req.user, ----PASS fb HERE--});
    })
}

有什么帮助吗?

尝试passReqToCallback: true,在req对象中添加一些东西,在后面的中间件中使用它

小心!这将在 request/response 循环结束后清除!所以例如如果您在 /oauth/start 的请求中设置 req.tmpPassport,它重定向到 facebook 并返回到 /oauth/endreq.tmpPassport 将再次未定义。如果您需要跨重定向跟踪某些内容,请改用 req.session

passport.use(new FacebookStrategy({
    passReqToCallback: true,
    clientID: config.fb.appID,
    clientSecret: config.fb.appSecret,
    callbackURL: config.fb.callbackURL,
    profileFields: ['id', 'displayName', 'photos', 'birthday', 'events', 'profileUrl', 'emails', 'likes']
}, function(req, accessToken, refershToken, profile, done){
    req.tmpPassport = {};
    var fb = new fbgraph.Facebook(accessToken, 'v2.2');
    fb.me(function(err, me) {
        req.tmpPassport.me = me;
    });
    fb.my.events(function(err, events) {
        req.tmpPassport.events = events;
    });
    fb.my.friends(function(err, result){
        req.tmpPassport.results = results;
    });

}))