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 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: Email Password Signup Date The contents of the users.js will be as follows: mongoose = ( ); bcrypt = ( ); UserSchema = mongoose.Schema({ : { : , : }, : { : , : }, : { : , : }, : { : , : .now() } }); UserSchema.methods.generateHash = { bcrypt.hashSync(password, bcrypt.genSaltSync( ), ); }; UserSchema.methods.validPassword = { bcrypt.compareSync(password, .password); }; .exports = mongoose.model( , UserSchema); const require 'mongoose' const require 'bcrypt' const new email type String default '' password type String default '' isDeleted type Boolean default false signUpDate type Date default Date ( ) function password return 8 null ( ) function password return this module 'User' 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 in order for us to store the sessions every time the user gets logged in. The code will be as follows: userSessions.js mongoose = ( ); userSessionSchema = mongoose.Schema({ : { : , : }, : { : , : .now() }, : { : , : } }); .exports = mongoose.model( , userSessionSchema); const require 'mongoose' const new userId type String default '' timestamp type Date default Date isDeleted type Boolean default false module 'UserSession' 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 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. signup.js 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: app.post( , (req, res, next) => { { body } = req; { password } = body; { email } = body; (!email) { res.send({ : , : }); } (!password) { res.send({ : , : }); } email = email.toLowerCase(); email = email.trim(); User.find({ : email }, (err, previousUsers) => { (err) { res.send({ : , : }); } (previousUsers.length > ) { res.send({ : , : }); } newUser = User(); newUser.email = email; newUser.password = newUser.generateHash(password); newUser.save( { (err) { res.send({ : , : }); } res.send({ : , : }); }); }); }); '/api/account/signup' const const let if return success false message 'Error: Email cannot be blank.' if return success false message 'Error: Password cannot be blank.' //Verify email doesn't exist email if return success false message 'Error: Server error' else if 0 return success false message 'Error: Account already exist.' // Save the new user const new ( ) => err, user if return success false message 'Error: Server error' return success true message 'Signed up' // end of sign up 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: app.post( , (req, res, next) => { { body } = req; { password } = body; { email } = body; (!email) { res.send({ : , : }); } (!password) { res.send({ : , : }); } email = email.toLowerCase(); User.find({ : email }, (err, users) => { (err) { res.send({ : , : }); } (users.length!= ) { res.send({ : , : }); } user = users[ ]; (!user.validPassword(password)){ res.send({ : , : }); } userSession = UserSession(); userSession.userId = user._id; userSession.save( { (err) { res.send({ : , : }); } res.send({ : , : , : doc._id }); }); }); }); '/api/account/signin' const const let if return success false message 'Error: Email cannot be blank.' if return success false message 'Error: Password cannot be blank.' email if return success false message 'Error: Server error' if 1 return success false message 'Error: Invalid.' const 0 if return success false message 'Error: Account already exist.' //save userSession const new ( ) => err, doc if return success false message 'Error: Server error' return success true message 'Valid Signin.' token //end of sign in 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… User = ( ); UserSession = ( ); .exports = { app.post( , (req, res, next) => { { body } = req; { password } = body; { email } = body; (!email) { res.send({ : , : }); } (!password) { res.send({ : , : }); } email = email.toLowerCase(); email = email.trim(); User.find({ : email }, (err, previousUsers) => { (err) { res.send({ : , : }); } (previousUsers.length > ) { res.send({ : , : }); } newUser = User(); newUser.email = email; newUser.password = newUser.generateHash(password); newUser.save( { (err) { res.send({ : , : }); } res.send({ : , : }); }); }); }); app.post( , (req, res, next) => { { body } = req; { password } = body; { email } = body; (!email) { res.send({ : , : }); } (!password) { res.send({ : , : }); } email = email.toLowerCase(); User.find({ : email }, (err, users) => { (err) { res.send({ : , : }); } (users.length!= ) { res.send({ : , : }); } user = users[ ]; (!user.validPassword(password)){ res.send({ : , : }); } userSession = UserSession(); userSession.userId = user._id; userSession.save( { (err) { res.send({ : , : }); } res.send({ : , : , : doc._id }); }); }); }); app.get( , (req, res, next) => { {query} = req; {token} = query; UserSession.find({ : token, : }, (err, sessions) => { (err){ res.send({ : , : }) } (sessions.length!= ){ res.send({ : , : }) } { res.send({ : , : }) } }); }); app.get( , (req, res, next) => { {query} = req; {token} = query; UserSession.findOneAndUpdate({ : token, : }, { :{ : } }, , (err, sessions) => { (err){ .log(err); res.send({ : , : }); } res.send({ : , : }); }); }); }; const require '../../models/users' const require '../../models/usersessions' module ( ) => app /* * Sign up */ '/api/account/signup' const const let if return success false message 'Error: Email cannot be blank.' if return success false message 'Error: Password cannot be blank.' //Verify email doesn't exist email if return success false message 'Error: Server error' else if 0 return success false message 'Error: Account already exist.' // Save the new user const new ( ) => err, user if return success false message 'Error: Server error' return success true message 'Signed up' // end of sign up '/api/account/signin' const const let if return success false message 'Error: Email cannot be blank.' if return success false message 'Error: Password cannot be blank.' email if return success false message 'Error: Server error' if 1 return success false message 'Error: Invalid.' const 0 if return success false message 'Error: Account already exist.' //save userSession const new ( ) => err, doc if return success false message 'Error: Server error' return success true message 'Valid Signin.' token //end of sign in '/api/account/verify' const const _id isDeleted false if return success false message 'Err: Server Error' if 1 return success false message 'Err: Server Error' else return success true message 'No Error' //end of Verify '/api/account/logout' const const _id isDeleted false $set isDeleted true null if console return success false message 'Err: Server Error' return success true message 'No Error' Now just run the file and use postman to send the post and get requests and we can see the user details in Mongo Compass. signing.js 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.