Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails.js + passport.js : managing sessions

I am trying to implement a facebook connection in sails using passport. Therefore, I've created a passport.js file in my services folder, the code is given below. It looks like the login is done successfully, however the user serialization doesn't seem to work as the console.log that I put in it never appears in the console and I cannot access the user id trhough req.user once the user is supposed to be logged in. Did anyone managed to get passport working with sails?

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);
        });
      }
    });
  }
));
like image 315
adc06 Avatar asked Aug 26 '13 16:08

adc06


2 Answers

While I do not have a direct answer for you, this was extremely useful to when getting it to work with GitHub OAuth: https://github.com/stefanbuck/sails-social-auth-example/blob/master/config/middleware.js

This is an entire, recent, Sails.js application implementing passport so it might be of use to you to side-by-side the two in the debugger and find out what is going on.

like image 133
John Postlethwait Avatar answered Sep 30 '22 14:09

John Postlethwait


Check out this easy and full implementation for sails.js with passport.js supporting both Email, Twitter and Facebook.

  • https://github.com/bmustata/sails-auth-super-template
like image 43
bmustata Avatar answered Sep 30 '22 14:09

bmustata