使用passport-facebook将accessToken保存到本地存储

时间:2015-12-22 04:27:16

标签: node.js passport-facebook passport.js

我是MEAN Stack的新手,我无法将passport-facebook accessToken保存到localStorage。我该怎么做呢?以下是我的设置。

 passport.use(new FacebookStrategy({
        clientID: passport_config.facebook.clientID,
        clientSecret: passport_config.facebook.clientSecret,
        callbackURL: passport_config.facebook.callbackURL
      },
      function(accessToken, refreshToken, profile, done) {
        FBAccount.findOne({fbId : profile.id}, function(err, oldUser){
            if(oldUser){
                done(null,oldUser);
            }else{
                var newUser = new FBAccount({
                    fbId  : profile.id ,
                    name  : profile.displayName
                }).save(function(err,newUser){
                    if(err) throw err;
                    console.log(newUser);
                    done(null, newUser);
                });
            }
        });
      }
    ));

2 个答案:

答案 0 :(得分:1)

试试这个

var localStorage = require('localStorage')

localStorage.setItem('accessToken', accessToken);
FBAccount.findOne({ ....

答案 1 :(得分:0)

您还可以通过某些中间件(如

)在Cookie中添加令牌
passport.use(new FacebookStrategy({
   clientID: '566950043453498',
   clientSecret: '555022a61da40afc8ead59c6c26306ed',
   callbackURL: 'http://www.localhost:3000/auth/facebook/callback'
}, function(accessToken, refreshToken, profile, done) {
    console.log("hello " + profile.displayName);
    done(null); 
 }
));

//Authentication
app.get('/auth/facebook', passport.authenticate('facebook'));

router.get('/auth/facebook/callback', passport.authenticate('facebook', {
    failureRedirect: '/login?failedSocial=facebook'
  }), auth.authCallback);

并在身份验证服务中

exports.authCallback = function (req, res) {
    res.res.cookie('token', JSON.stringify(req.user.token));
    res.redirect('/');
}