Passport Oauth无法将用户序列化为会话

时间:2019-02-22 09:36:56

标签: javascript node.js

我一直遇到错误

  

错误:无法将用户序列化为会话

及其与此行对应的

  

/config/passport-github.js:12:5

哪个是user.id,我认为这是将其存储到会话中的正确方法。不知道为什么它不起作用。感谢帮助

passport-github.js

require('dotenv').config();


module.exports = function(passport) {
const GitHubStrategy = require('passport-github').Strategy;
const models = require( '../models/index');
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  // from the user id, figure out who the user is...
  passport.deserializeUser(function(userId, done){
    models.User
      .find({ where: { id: userId } })
      .then(function(user){
        done(null, user);
      }).catch(function(err){
        done(err, null);
      });
  });
  passport.use(new GitHubStrategy({
      clientID: process.env.clientID,
      clientSecret: process.env.secret,
      // if the callback is set to 5000 the 0auth app will not work for some reason
      callbackURL: 'http://127.0.0.1:8000/api/users/auth/github/callback',
      passReqToCallback: true,


    },
    function(accessToken, req, token, refreshToken, profile,done) {
      // successfully makes a new user with id, and username equivalant to github username
        models.User
        .findOrCreate({
          where: {
            [Op.or]: [
              {
                id: profile.id,
                username:profile.username
              }
            ],
          }
        })
        .then( (user, created) => {
           return done(null, user, profile, token, accessToken)
        });      
    }
  ));




};

0 个答案:

没有答案
相关问题