转移使用' passport'创建的javascript var表达路由器

时间:2015-07-16 17:29:36

标签: javascript node.js express passport.js

我创建了这个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传递给routes文件并将其传递给render函数。

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

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--});
    })
}

有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

尝试passReqToCallback: true,向req对象添加内容,并在以后的中间件中使用

小心!在请求/响应周期结束后,这将清除!所以例如如果您在req.tmpPassport的请求中设置/oauth/start,并且重定向到Facebook并返回/oauth/end,则req.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;
    });

}))