Here are my register and login setup which I used to authenticate users using passport-local-mongoose , passport-local ,passport, express-session
app.post("/register",(req,res)=>{
User.register({username:req.body.username},req.body.password,(err,user)=>{
if(err){
res.redirect("/register")
} else{
passport.authenticate('local')(req,res,()=>{
res.redirect("/secrets")
})
}
})
});
app.post("/login",(req,res)=>{
const user=new User(req.body)
req.login(user,(err)=>{
if(err){
console.log(err)
res.redirect("/login")
} else{
passport.authenticate('local')(req,res,()=>{
res.redirect("/secrets")
})
}
})
})
Whenever authentication is performed and in the following cases if username already exists(register) , incorrect username or password(login) these application don't send custom error messages like username already exists, wrong id/password .
How can I implement that and send those messages to ejs file and how can I access them in my template?