I am using :
For each user, I store in a MySQL DB (
I used node.js This is code from documentation :
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
and this is the code, I modified to understand better :
var passport = require ('passport')
passport.serializeUser (function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
var user = _.find(fixtures.users, 'id', id)
if (user) {
done (null, user);
}
else
{ done (null, false) }
});
module.exports = passport;
In serializeUser, client sending cookie with an 'id'. Here '(user)' argument in serializeUser is user profile and client using only 'id' from user profile to send request to server. done callback passing user.id to server.
In deserializedUser, server will look into database and find the user with that 'id' from serializedUser and return the user profile. I used 'lodash' to use find and remove methods.
var _ = require ('lodash')
var user = _.find(fixtures.users, 'id', id)
find will look for user id and return user profile with that id . More details about lodash is here : lodash . After that I am checking if user exist then return user otherwise return false.
At the end, I am exporting passport.
module.exports = passport;