User Registration API using MongoDB
Learning MongoDB has been one of the things in my checklist for the past 6 months but hasn’t gotten around actually learning it until now. In order to understand writing queries and all I figured it’ll be better to make something instead of just reading and watching some tutorials. So, I’ve decided to make a user registration API i.e, a simple registration form kinda thing with only sign up and sign in functions as of now. As learning Mongo was the main intention, I’ve decided just to make the core app which is the backend functionality and no front end as it’s not necessary and I hate to do it.
First install the required modules which are mongoose, express, and bcrypt.
npm init //this will generate the JSON file
npm install express mongoose bcrypt
Now make a folder to keep all the core files and create users.js and userSessions.js file where users.js contains the user schema and the later one holds the user sessions schema.
Now first write the users schema involving the following attributes:
The contents of the users.js will be as follows:
I guess the schema part is self-explanatory and as for the bcrypt part first, we’ve to create a generateHash method that’ll encrypt the password and then another method validPassword to compare the password encrypted before to the password entered.
Now let’s fill the file userSessions.js in order for us to store the sessions every time the user gets logged in. The code will be as follows:
Now to the crucial part, Signup… sign up will be the first thing we do as it is difficult to log in if a user has not signed up. This will involve creating a new file, adding an endpoint and changing the frontend to submit email and password. This involves creating file signup.js and this file will be handling almost the entire functionality like signing up, verifying email address, saving the user data, sign in, verifying the sign in and log out.
First, the signup part where we prompt the user to enter email and password then we’ll verify if the email already exists or not and if it exists we’ll say that it does or else we’ve to save the new user details. So, the code will be as follows:
Now onto the sign-in part again it’s divided into 3 parts, first, accept the data user has entered then verify whether the email already exists and if it does then save the user session or else pop a message that the user doesn't exist and needs to signup before. And the code for that will be:
Now the only thing left is to handle the logout and as we’ve seen that there’s a boolean attribute “isDeleted” which was set to false now we just need to change it to true as it clears the session. The code is as follows:
Now let's put together the entire code…
Now just run the signing.js file and use postman to send the post and get requests and we can see the user details in Mongo Compass.
I know this is the most basic use of MongoDB but still, it’s a start and I want to dive deep into more concepts so that I can implement stuff that involves handling user-profiles and all.