sessionmanager.js 867 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. function SessionManager(options, serializeUser) {
  2. if (typeof options == 'function') {
  3. serializeUser = options;
  4. options = undefined;
  5. }
  6. options = options || {};
  7. this._key = options.key || 'passport';
  8. this._serializeUser = serializeUser;
  9. }
  10. SessionManager.prototype.logIn = function(req, user, cb) {
  11. var self = this;
  12. this._serializeUser(user, req, function(err, obj) {
  13. if (err) {
  14. return cb(err);
  15. }
  16. if (!req._passport.session) {
  17. req._passport.session = {};
  18. }
  19. req._passport.session.user = obj;
  20. if (!req.session) {
  21. req.session = {};
  22. }
  23. req.session[self._key] = req._passport.session;
  24. cb();
  25. });
  26. }
  27. SessionManager.prototype.logOut = function(req, cb) {
  28. if (req._passport && req._passport.session) {
  29. delete req._passport.session.user;
  30. }
  31. cb && cb();
  32. }
  33. module.exports = SessionManager;