Search code examples
javascriptnode.jsexpresspassport.js

Node and Passport Handling Username Already Existing


I am trying to get my code to handle a username already existing. I know that passport does that automatically with a console.log(err) however, I would like to flash the user a message if that happens, so I needed to add a bit more code to do it. It seems to work, however, I am getting a lot of console errors when I implement my code this way and I would like to avoid any errors in the future. Here is my code:

router.post("/", isLoggedIn, isAdministrator, async function(req, res){ 
    let users = [];
    let newUser = new User({
        username: req.body.user["username"],
        first_name: req.body.user["first_name"],
        middle_name: req.body.user["middle_name"],
        last_name: req.body.user["last_name"],
        email_address: req.body.user["email_address"],
        phone_number: req.body.user["phone_number"],
        street: req.body.user["street"],
        city: req.body.user["city"],
        state: req.body.user["state"],
        zip: req.body.user["zip"],
        user_permissions: req.body.user["user_permissions"],
    });
    try {
        users = await User.find({});
    }
    catch (err) {console.log(err);} 

    users.forEach(function(user) {
        if (user.username == newUser.username){
        // flash username already exists
        console.log("User Already Exists")
        return res.redirect("/users/add");
        }
    });
// If username does not exist
    try {
        await User.register(newUser, req.body.user["password"]);
    }
    catch (err) {console.log(err);} 

    res.redirect("/users");
});

And here is what the console is logging

User Already Exists
ctor [UserExistsError]: A user with the given username is already registered
    at ...\node_modules\passport-local-mongoose\index.js:237:17
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ...\routes\users.js:93:3
(node:4780) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:518:11)
    at ServerResponse.header (...\node_modules\express\lib\response.js:771:10)
    at ServerResponse.location (...\node_modules\express\lib\response.js:888:15)
    at ServerResponse.redirect (...\node_modules\express\lib\response.js:926:18)
    at ...\routes\users.js:97:6
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:4780) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4780) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Like I said, the logic of this code still works fine, but I am concerned about the promise rejection part of the code. It seems that it is still trying to handle the last res.redirect("/users") in the last line, despite me already returning a res.redirect earlier. Is there a reason for this, and should I be concerned about it? Is there a better way of implementing a flash if the username already exists using passport, without my separate forEach loop? Thanks.


Solution

  • This not an answer to your problem but I feel obliged to mention this:

    users.forEach(function(user) {
       if (user.username == newUser.username){
        // flash username already exists
        console.log("User Already Exists")
        return res.redirect("/users/add");
       }
    });
    

    Instead of doing the above, why don't you just have the DB system do that for you instead of iterating through a list of users in memory? Like so:

    users = await User.find({ username: { $eq: newUser.username }});