I need do hide the login and signup buttons once the user is authenticated. My login is working well, but when I create the if statement, it does work as supposed. It is always showing both the login and signup buttons, even when the login is done, which leads me to believe that I'm not writing the condition well (which I've assigned as user). Does it have to do with wrong variable name or am I missing something? Any help is welcome.
layout.hbs:
<form action="/logout" method="POST" id="form">
<button type="submit">Log Out</button>
</form>
<a href="/login">Log In</a>
<a href="/signup">sign up</a>
auth-routes.js:
router.get("/login", (req, res, next) => {
res.render("auth/login");
});
router.post("/login", (req, res, next) => {
console.log('loggin in!');
const theUsername = req.body.username;
const thePassword = req.body.password;
console.log(req.body)
if (theUsername === "" || thePassword === "") {
res.render("auth/login", {
errorMessage: "Please enter both, username and password to sign up."
});
return;
}
User.findOne({ "username": theUsername })
.then(user => {
if (!user) {
res.render("auth/login", {
errorMessage: "The username doesn't exist."
});
return;
}
if (bcrypt.compareSync(thePassword, user.password)) {
// Save the login in the session!
req.session.currentUser = user;
res.redirect("/list");
console.log('Done!');
} else {
console.log('undone!');
res.render("auth/login", {
errorMessage: "Incorrect password"
});
}
})
.catch(error => {
next(error);
})
});
Aucun commentaire:
Enregistrer un commentaire