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