how to make login logic ( using Sequelize )

Issue

login proc that i thought is…

  1. put nickname, User_pw in req.body.
  2. using findByPk, check specific data.
  3. if User_pw in data is same with req.body.User_pw,
    login success.

can i using this logic?
how to use found data?
( like… if i found data using findByPk, i wanna compare User_pw in data with req.body.User_pw )

this is code…

> router.post('/login', function(req,res,next){  
  var nickname = req.body.nickname  
  var User_pw = req.body.User_pw  
  db.Normal_user.findByPk(nickname).then((res) => {  
    console.log(res)  
  }).catch((err) => {  
    res.json({  
      success : false  
    }), console.log(err)  
  })  
})  

Solution

You can search for the row where both, username and password, are the ones inserted.
In this case, you can simply check if you get any user. If you don’t get any, it means that there is no user with the received username and password and you can return an message like: Username or password incorrect

router.post('/login', async(req,res,next){  
  var nickname = req.body.nickname  
  var User_pw = req.body.User_pw  
  const user = await db.Normal_user.findOne({
    where: {
      nickname: nickname,
      User_pw: User_pw
    }
  });
  if(!user){
    res.status(402).json({
        success: false
    })
  }else{
    res.status(200).json({
        success: true
    })
 }
})

Answered By – Alexandru DuDu

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.