I'm currently exploring the usage of passport and I am in the process of setting up a "register" page functionality. The registration process is working smoothly, along with the log-in form. Yet, I am looking to implement a validation to check if the username entered already exists in the system. In case of a duplicate username, I aim to provide an error message to the user. Below is the snippet of code I have developed so far:
expressApp.post("/register", function(request, response){
User.findOne({username: request.body.username}, function(err, user) {
if (err) {
return err;
}
if (user) {
}
else {
User.register(new User({
username: request.body.username,
type: "Student"}),
request.body.password, function(err){
if(err){
console.log(err);
}
passport.authenticate("local")(request, response, function(){
response.redirect("/");
});
});
}
})
});
My goal is to notify the user with an error message if they attempt to register with a username that already exists within the system.