Sails.js + passport.js: session management

I am trying to implement a facebook connection in sails using a passport. Therefore, I created the passport.js file in my services folder, the code is below. The login seems to be successful, however the serialization of the user does not work, because the console.log that I inserted into it never appears in the console, and I cannot access the user id trhough req.user as soon as the user is allowed to entrance. Has anyone been able to get a 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); }); } }); } )); 
+7
source share
2 answers

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

This is a complete, recent Sails.js application that implements a passport, so it may be useful for you to be next to each other in the debugger and find out what is happening.

+8
source share

Check out this simple and complete implementation for sails.js with passport.js , supporting both email, Twitter, and Facebook.

+2
source share

All Articles