0

I am working on the login controller of my web app, especially if a user want to log without an existing mail in the DB. I can't reach the res.status in the frontend in order to handle this specific response.

My backend:

exports.signin = (req, res) => {
        const email = req.body.email;
        const password = req.body.password;
        //Recherche du compte associé au email pour vérifié le mdp
       db.query("SELECT password FROM users WHERE email = ?", email, (err, result) => {
           if(result.length === 0){
                    console.log(err)
                    res.status(401).json({err})                
                } else {
                    console.log(result)  
                    let encryptedPassword = result[0].password
                    console.log(encryptedPassword)
                    let verifyPassword = bcrypt.compareSync(password, encryptedPassword)
                    //Vérification du MDP
                    if (verifyPassword === true) {
                        //Requete SQL pour assigné un token en fonction de l'id qui correspond à l'utilisateur dans la BDD
                        db.query("SELECT id, prenom FROM users WHERE email = ?" , email, (err, result) => {
                            if(result){
                                let userID = result[0].id
                                let userPrenom = result[0].prenom
                                console.log(userID)
                                console.log('Le mot de passe correspond à celui renseigné dans la DB')
                                res.status(200).json({user_id: userID,
                                                    prenom:  userPrenom,
                                                    token: jwt.sign(
                                                        {user_id: userID},
                                                        'RANDOM_TOKEN_SECRET',
                                                        { expiresIn: '24h'})})
                            }
                        })}
                    else{
                        res.status(208).send({message: 'pbl'})
                        console.log('Le mot de passe ne correspond pas !')
                    }             
                }           
            }
        )       
    }

I used to have if(err){console.log(err)} but my BACKEND kept crashing whenever I try an unknown email.

My frontend:

const login = () => {
    Axios.post('http://localhost:3001/auth/signin', {
      email: MailLog,
      password: userPasswordLog,
    })
    .then((response) => {
    //Récupérartion des informations user
    if (response.status === 200){
        console.log(response)
        setLoginStatus("Bonjour " + response.data.prenom + " !")}
    else if (response.status === 401) {
        console.log(response)
        setLoginStatus("Nous ne trouvons pas de compte associé à cette adresse mail")}
    else {
        console.log(response)
        setLoginStatus('Le mot de passe et l\'adresse mail ne correspondent pas !')}
    })
    }

What would be the best way to fix my problem? Thank you

NB: The status code is changing when I change it in my res.status yet I can't do anything with it

El_Michel
  • 11
  • 5
  • So your frontend or your backend is crashing? If your frontend crashes, you proably should also show some code of the fontend. I see nothing out of the ordinary in your (far to small) backend snippet. and the shown response seem consistend with this backend code – derpirscher Nov 30 '21 at 20:34
  • I've edited my post – El_Michel Nov 30 '21 at 20:41
  • Yes, you are right. returing a 2xx even if ther was an error has kind of a smell. Have a look at the following question which will show you proper ways to deal with such a situation https://stackoverflow.com/questions/39153080/how-can-i-get-the-status-code-from-an-http-error-in-axios – derpirscher Nov 30 '21 at 20:55
  • And just as a hint: There is no need for querying the same table twice. You can include all needed user properties in the first query. Adding two more properties to the query result will almost certainly be much faster then querying twice. And it saves you an additional level in callback-hell (btw. have you thought about using a mysql library that support promises. This will make your code much more readable) – derpirscher Nov 30 '21 at 21:09
  • Yes I'm aware that my code isn't optimised. This project introduces me to node.js/react/mysql and I still a bit confuse about them. Thanks for your advice and your time – El_Michel Nov 30 '21 at 21:34

0 Answers0