sails.js + passport.js:管理会话

时间:2013-08-26 16:20:10

标签: sails.js passport.js

我正在尝试使用护照在帆中实现facebook连接。因此,我在我的services文件夹中创建了一个passport.js文件,代码如下。看起来登录成功完成,但是用户序列化似乎不起作用,我放入的console.log从未出现在控制台中,一旦用户被认为我无法访问用户ID trhough req.user登录。有没有人设法让护照与帆一起工作?

var  passport = require('passport')
  , FacebookStrategy = require('passport-facebook').Strategy,
  bcrypt      = require('bcrypt');

// helper functions
function findById(id, fn) {
  User.findOne(id).done( function(err, user){
    if (err){
      return fn(null, null);
    }else{
      return fn(null, user);
    }
  });
}

function findByUsername(u, fn) {
  User.findOne({
    username: u
  }).done(function(err, user) {
    // Error handling
    if (err) {
      return fn(null, null);
    // The User was found successfully!
    }else{
      return fn(null, user);
    }
  });
}

// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function(user, done) {
  console.log("utilisateur serilizé!");
  done(null, user.uid);
});

passport.deserializeUser(function(id, done) {
  //console.log("coucou");
  findById(id, function (err, user) {
    done(err, user);
  });
});


// Use the LocalStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a username and password), and invoke a callback
// with a user object.


// using https://gist.github.com/theangryangel/5060446
// as an example


passport.use(new FacebookStrategy({
    clientID: 'XXX',
    clientSecret: 'XXX',
    callbackURL: "http://localhost:1337/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOne({uid: profile.id}, function(err, user) {
      if (err) { return done(err); }
      if (user) {
        //console.log('momo');
        User.update({uid : user.uid},{token : accessToken},function(){done(null, user);});
         } else {
        console.log(profile);
        var user_data = {
          token : accessToken
          , provider:   profile.provider
          , alias:  profile.username
          , uid:      profile.id
          , created:  new Date().getTime()
          , name: {
            first:  profile.name.givenName
            , last: profile.name.familyName
          }
          , alerts: {
              email:      true
            , mobile:   false
            , features: true
          }
        };
        console.log(user_data);
        User.create(user_data).done(function(err, user) {
          console.log(err);
          if(err) { console.log("err");throw err; }
          done(null, user);
        });
      }
    });
  }
));

2 个答案:

答案 0 :(得分:8)

虽然我没有直接的答案,但这对于使用GitHub OAuth非常有用:https://github.com/stefanbuck/sails-social-auth-example/blob/master/config/middleware.js

这是一个完整的,最近的Sails.js应用程序,它实现了护照,所以它可能对您在调试器中并排使用它们并找出正在发生的事情。

答案 1 :(得分:2)

使用支持电子邮件,Twitter和Facebook的 passport.js 查看 sails.js 的简单完整实施。