On signup I check the database to see if the username and email exists if they exist it should return an error. all usernames and emails should be unique. It seems that my problem is that I could only get one error. If the email exists already I get the flash message but if the username also exists it doesn't say anything about the username already existing. I want to display both errors If both exist already on post in a flash message
I'm assuming the problem is returning done()
for the email. since I have 2 queries It only looks at the first one that was hit. I don't really know.
I was trying to do research on this and I know that you could pass in an array for req.flash but I just could get that working because it's returning the first one it sees that is existing I think.
I also read that the third parameter for done
is an info object that I think gives info about the user and I was thinking of loading the existing information there but I just couldn't put it together. I tried other stuff too. but I'll just show you the stuff that works with the querying the database because I failed horribly
passport.use("local-signup", new LocalStrategy({
usernameField : "email",
passwordField : "password",
passReqToCallback : true
},
function(req, email, password, done){
var arr = [];
User.findOne({"email" : email}, function(err, user){
if(err) return done(err);
if(user){
// arr.push("That email is already taken")
return done(null, false, req.flash("signupMessage", "That username is taken"))
}else{
User.findOne({"username" : req.body.username}, function(err,user){
if(err) return done(err);
if(user){
// arr.push()
return done(null, false, req.flash("signupMessage", "That username is taken"))
}else{
var newUser = new User();
newUser.username = req.body.username;
newUser.password = password;
newUser.email = req.body.email;
newUser.save(function(err, doc){
if(err) throw err;
console.log("doc", " " , doc)
return done(null, newUser);
})
}
})
}
})
}
))
EJS
app.get("/signup", function(req, res){
console.log(req.session)
console.log(req.flash("signupMessage"))
res.render("signup", {authed : authed, user : user, message: req.flash("signupMessage")})
})
app.post("/signup",passport.authenticate("local-signup", {
successRedirect : "/",
failureRedirect : "/signup",
failureFlash : true
}))