Part 7…
So now we will start the somewhat tedious project of user sign up validation, which isn’t terrible, but then we will be setting up some production grade Authentication… but back to the task at hand! To explain a little about how I am going to approach this sign up process…. Whenever a user has filled out our sign up form and submits the form to the back end, we are going to run a few checks inside of the request handler. First, we check to see if another user has signed up with the provided email. There should be only ONE unique email per user, if the email is already taken, we’ll send a messages “email already exists”, remember, this is for signing up, not logging in. After that we want to check that both input fields for password (password, password conformation) match one another, If they don’t match we’ll relay the message of “passwords must match”. If both of these checks pass, then we will set up the user with an account, but we aren’t yet concerned with that. So to implement the checks, first Ill navigate to my index.js file and here we find the post handler…
Here we can see that we are logging out req.body… This is an object that contains all the properties inside of the form, specifically the name of the different inputs. For reference go a head and quickly run this file and check what is getting logged out…. Head back to your terminal, and in the main level of the project, run “node index.js” you should first see “Listening on port 3000” or whatever specific message that you created that shows that the server is up and running. Then navigate to localhost:3000 in your browser, and you should see the simple form we created. Now go ahead and sign-up a new user and check the console…
When you hit submit, you should see “Account Created!!” in the browser…
and in the console, the req.body has been logged out…
Here we can see that indeed our input field(keys) are assigned new values, ‘newUser’, “pass”, ‘pass’. This is exactly the object that we are hoping for. Now we can continue with the quick sign-up validations which will take email and password and passwordConformation properties and compare what is already stored in the database for emails and will check the input fields to ensure the correct spelling of both passwords. So now, head back to the post handler and instead of just logging out req.body lets make some adjustments. First, lets structure the email, password, and passwordConformation properties…
Next, we need to take that email, and check to see if it is indeed unique. To do that, we will need access to the users repository inside of our index file, so back at the top of the file require the repositories directory and now we have access to all the methods we had previously filled out…
As you can see on line 3, I named it usersRepo and we pull it from repositories/users, and now we have access to the instance of the repository. Now, back inside of the post route, its time to write the logic, for the check. So, whenever we need to gather data to do some comparisons or we want to somehow manipulate that data, its always best to make a copy of that data and then manipulate the copy(most of the time) so, start by naming the variable whatever you would like, but since we are checking to see if a user already exists… existingUser seems appropriate, and we are going to want to set that equal to await usersRepo.getOneBy({ email })…
What is happening here..? Well, like I stated. We need access to the users repository so that we can iterate over ALL the users to check every “email” property, and look for a matching value. The first part is done, now whats left is writing the logic that will actually do the comparisons and then return a message that says user already exists or a prompt that a new account was created. Using an if statement here seems about right…
Here, we want to take existing user, which gives us access to all the instances of a user, and we are pull the { email } property and run the check, if the email exists in the repository, we take the result(res) and send back the message “Email already exists”. Now if it doesn’t… we already have the logic written, res.send(Account created!!). Before we get too much further, whenever we want to “await” for a process to resolve before moving on, we need to make it an “async” function…
Now to test… If you dont have a user in your user.json file, go ahead and create a simple one…
Now, navigate back to localhost:3000, try to sign up using the same email that exists, and if the logic is correct…
So now, lets do the same thing for password comparisons. We are going to check “password” and “passwordConformation” against one another, if they do not match, we need to return out of the function immediately…
Here we are saying, if password is NOT equal to passwordConfirmation, send the message, “Passwords must exist”… Test once more this time use a new email and mispell one of the passwords…
And the result…
Now try to create a new one with all the correct criteria…
Now check your user.json file to ensure the new instance of a user was saved to our file…
There we have it! A new user instance was created, verifying unique emails, and password validations. That wraps up this “quick” validation and next we will start to tackle production grade Authentication!