Javascript 设置passport local:无法将用户序列化到会话中

Javascript 设置passport local:无法将用户序列化到会话中,javascript,node.js,mongodb,express,passport.js,Javascript,Node.js,Mongodb,Express,Passport.js,堆栈用途:Mongo、Express、React、NodeJS 我正在尝试使用本地设置passport,但是当我尝试使用postman时,我遇到了此错误 无法将用户序列化到会话中 我已经用google/facebook/twitter设置了Oauth,当用户以这种方式登录时,serialize可以很好地工作,关于这个主题的所有其他问题都推荐我已经拥有的相同代码 这是我的代码示例,也是我试图与邮递员一起发布的帖子。(我试过带id和不带id) passport.js //require statem

堆栈用途:Mongo、Express、React、NodeJS

我正在尝试使用本地设置passport,但是当我尝试使用postman时,我遇到了此错误

无法将用户序列化到会话中

我已经用google/facebook/twitter设置了Oauth,当用户以这种方式登录时,serialize可以很好地工作,关于这个主题的所有其他问题都推荐我已经拥有的相同代码

这是我的代码示例,也是我试图与邮递员一起发布的帖子。(我试过带id和不带id)

passport.js

//require statements^^

//serialize+deserialize
passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user);
  });
});

//local register
passport.use(
  new LocalStrategy(
    function (username, password, done) {
      const existingUser = User.findOne({ email: username });
      if (existingUser) {
        return done(null, existingUser);
      }
      console.log(username);
      const user = new User({
        localId: generator.generate({length:10, numbers:true}),
        email: username, 
        password: password 
      }).save();
      done(null, user);
    }
  )
); 

//google register
passport.use(
  new GoogleStrategy(
    {
      clientID: keys.googleClientID,
      clientSecret: keys.googleClientSecret,
      callbackURL: '/auth/google/callback',
      proxy: true
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleId: profile.id, email: profile.emails[0].value });

      if (existingUser) {
        return done(null, existingUser);
      }
      
      const user = await new User({ 
        googleId: profile.id, 
        email: profile.emails[0].value, 
        profilePic: profile.photos[0].value,
        password: generator.generate({length:10, numbers:true}) 
      }).save();
      done(null, user);
    }
  )
);

//facebook
...

//twitter
... 


js(与用户auth相关的路由)

User.js

///requires^^^

//user model
const userSchema = new Schema({
  googleId: String,
  facebookId: String,
  twitterID: String,
  localId: String,
  email: { type: String, require: true },
  password: { type: String },
  date: { type: Date, default: Date.now },
  credits: { type: Number, default: 0 },
  plan: { type: Number, default: 1 },
  profilePic: String,
  verified: {type: String, default: 'False'}
});

//password hashing + compare...

module.exports = mongoose.model('users', userSchema);
index.js

//requires

mongoose.Promise = global.Promise;
mongoose.connect(keys.mongoURI);

const app = express();

app.use(bodyParser.json());
app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey]
  })
);
app.use(passport.initialize());
app.use(passport.session());

//check production/dev
//port 5000
邮递员要求

POST to http://localhost:5000/auth/local
Header: key:Content-Type value: application/json
body: raw
{
    "id": "oeranbijgvnoeafrjbneioaj",
    "username": "testing1",
    "password": "testing123"
}

POST to http://localhost:5000/auth/local
Header: key:Content-Type value: application/json
body: raw
{
    "id": "oeranbijgvnoeafrjbneioaj",
    "username": "testing1",
    "password": "testing123"
}