登录后用户 ID 不会保存到会话中

User ID won't save into session after a log in

我是编码新手,正在尝试编写我的第一个应用程序。 我的登录功能遇到问题。用户 id 在用户注册后保存到我的会话对象中,但在登录后不会保存到其中。我正在使用 MongoDB。 这是代码:

控制器

exports.register = function(req, res){
    let user = new User(req.body)
    user.register().then(()=>{
        // what do we do if the registration is succesfull - redirecting to the homepage with updated session data
        req.session.user = {username: user.data.username, avatar: user.avatar, _id: user.data._id}
        req.session.save(function(){
            res.redirect('/')})
            console.log(' After the registration usercontroller line nr 14 ', user.data._id)
    }).catch((regErrors)=>{
        regErrors.forEach(function(error){

            req.flash('regErrors', error)
        })
        req.session.save(function(){
            res.redirect('/')
        })
    })
    
}


exports.login = function(req, res){

    let user = new User(req.body)
    user.login().then(function(result){
        //saving data into a cookie
        req.session.user = {avatar: user.avatar, username: user.data.username, _id: user.data._id}
        req.session.save(function(){
            res.redirect('/')
            console.log('After the log in  line 51 userController', req.session)
        })
        
       
    }).catch(function(e){
        req.flash('errors', e)
        req.session.save(function(){
            
            res.redirect('/')
            // After the failed log in 
            console.log('After the failed log in line 61', req.session)
        })
    })

}

型号

User.prototype.login = function() {

  return new Promise((resolve, reject) => {
    this.cleanUp()
    usersCollection.findOne({
      username: this.data.username
    }).then((attemptedUser) => {
      if (attemptedUser && bcrypt.compareSync(this.data.password, attemptedUser.password)) {
        this.data.attemptedUser
        this.getAvatar()
        resolve("Congrats !")

      } else {
        reject("Invalid username/password")
      }
    }).catch(function() {

      reject("Please try again later.")

    })


  })


}

User.prototype.register = function() {
  return new Promise(async(resolve, reject) => {
    //Step #1: Validate user data
    await this.validate()
    this.cleanUp()

    //Step #2: Only if there are no validation errors save the user data into a database
    if (!this.errors.length) {
      // Hash user password
      let salt = bcrypt.genSaltSync(10)
      this.data.password = bcrypt.hashSync(this.data.password, salt)
      await usersCollection.insertOne(this.data)
      this.getAvatar()
      resolve()
    } else {
      reject(this.errors)

    }


  })
}

Screenshot of the console - registration

Screenshot of mongo - registration

Screenshot of the console - log in

Screenshot of mongo - log in

我一直在关注 Brad Schiffs Java Script Full Stack from Scratch 课程,一切似乎对他来说都很好。

您的模型未对找到的用户执行任何操作: 尝试

if(attemptedUser && bcrypt.compareSync(this.data.password, attemptedUser.password)) {
    this.data.attemptedUser = attemptedUser
    this.getAvatar()
    resolve("Congrats !")