Email Account Verification - Node and Express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to another video in this video I'm going to talk about email account verification using node and Xpress so I have caught this project that I have very so if you want to know that how to start writing so we're using node and Express and if you have never ever written node Express server then I have already created these series with the node Express a developer's guide and this is the loop back with angular and these are the nodes related video so I'm going to give the link of this whole video this list then you can actually go ahead and learn about how to setup Nolan Xpress mainly if you want to see then this is the main video that I would recommend you guys to actually go ahead and watch these videos who don't know how to start writing node what is NPM what is node because in this video I'll be talking about a big advanced topic which is email account verification once the users sign up and without his email account activation he should not proceed to find em okay so before moving further I just want to explain what does this already creative project do and what each file is created for so on the left side I have this controller or and this is the DB connect debido GS and models user dot J's models and this is the normal use as out or giorgia this is dot Ian we file for the environment variables getting your file package or JSON server dot Jason okay so this is the packet or JSON file and these are the dependencies that I have installed and this is the code I don't envx press J Express JWT and if you want to start writing this project make sure you pause this video and start writing this project on your own if you don't know anything about load and Xpress okay create these files with the same structure and you will be able to run this project this project doesn't contain email account verification it straightaway create an account in the database for the user without email activation okay I'll start off the server gorgeous and the server gorgeous I have required Express I have required called and I have required this Dottie and me config in order to use environment variable files and I have required this DB connect DB file this connectivity file and this will automatically required this file and that file is responsible to actually connect to the database okay so required Mongoose this is directly connecting to the database and I'm getting the name of the database and the path from the process in which database that I've created in dot env pipe in Dorney and me I have this board and I have this database equal to Momo DB : / / localhost colon 2000 0 1 7 slash and the name of database that is going to be created in my MongoDB okay so this is the connect DB file I'm going to close it and I'm going to close this file now and this is the server dot G and after that I'm creating an app using Express and now I am requiring the routes file that I'm going to show you in a minute and I have actually loaded this thing expert Jason because we want to parse the JSON data to the API of the server and without it it won't be able to get the JSON data okay so app dot you call this is for the other access it is to give the other host to actually access this REST API okay so this is a middleware this is these are the routes that I have required above and I'm going to show you and this is a processing and report and I'm now listening that both in order to run the server okay so if I go to the package or Jason this is a command that I have written make sure you install all these packages and add this script come on over here in order to run the server I will simply write NPM run start NPM run start okay all right so server is running on port 8000 okay this is because of this log this is port which is coming from dot PNG file for variable okay and in the database that DB connection established okay this is message coming from here okay and this is the name from the database in the dot en new file alright so now let's go to a middle will or routes what we have over here this is a route file and I'm requiring Xpress and this is how I am requiring the router and here I am creating an API route with the method post and this is the part of that API slash sign up and this is a controller controller is basically to minimize the code in these routes we can actually write all the controller's code within this routes file as well but the controller's purpose is to separate the logic of all the hard things happening once this route gets hit okay the flash API slash sign up which complete the whole REST API for signing up the user and this is of method post so the total API route would be localhost colon 8080 I like sign up okay and then I need to export it this router in order to be used over okay so this is the controller that I have actually required and I'm going to show you this controller and this controller actually contains the code for creating a new user in the database okay it doesn't actually implement the account activation as of now but I'm going to do it in this video okay so it actually logs this body I'm going to show you the model of user model this is the Mongols are required this is the package for creating the schema for any of the models so it contains name email and password I'm not doing any kind of validation as of now because that's a different topic and I might create another video to add the validation to these properties okay so name email and the password these three properties I was using or to create a new user so in this controller I am getting the data name email and password from requested body and this is I am receiving and here I'm first checking and that if the user already exists in DB or not if it exists then I'm returning a four hundred errors with a message that user with this email ID already exists if not that I'm creating a new user and I'm saving it in the database and if there is any error somehow in because of no reason then error shown up otherwise sign of success message will be returned okay so check out my other videos in order to know that how to connect to the how to run the Modi be in your local system so in my terminal here let me just close it and mom would D is the command that i will run once installed the momo DB make sure you add the path of that momo DB installation bin folder in the environment variables of your windows system okay so mom would be a command i have run in the terminal so now it is waiting mean to actually run the bongo server okay so actually to actually view how the database looks like what are the different properties of each models I'm using the Robo 3d of version 1.3 and here refresh and then now this actually this is actually connected okay if I go to the collections user so there is no user has been created as of now all right so now I'm not going to be using any front-end instead I'm going to be using postman in order to call the API so this is the URL which is localhost colon 8080 over here if I go to edit this is a variable this is the variable that I have created that I can use using double curly braces okay so URL slash appears like sign up so my server is running in the terminal okay and for header I have added content type application slash Jason except application slash Jason body and name this is the email ID let me this is the email ID and this is the password that I have added and this is the raw data and this is of the Jason type okay so if I click on it it's going to call that API and it's going to return with this method sign of success okay so this is the message that it has about and it has log this data from here this is the log let me go to the database and I'm going to refresh click on user and this is the user that has been created and this is the same data that I sent alright okay so this data is being created this is a user's data that is being created without any account activation for these users okay I hope that you have got the idea how I have structured this tiny project okay so now I'm gonna proceed further in order to actually activate the users account before it can actually store the users data in the database all right so I'm going to just drop this collection user okay and for now I'm just going to copy this function and I'm going to paste it right below and I'm going to comment this above one okay all right so now first of all what I needed I need some kind of our email server okay that let us and the emails to the respective recipient so there are a lot of these kinds of things so one of them I'll be using is this mail gun and mail gun is the NPM package that I'm going to be using some people use SendGrid for sending the emails so it up to you whatever you want to use so once you create a new account on the mail gun what you need to do is go to this sendings alright and this is the one the name the domain name one will be automatically created for you okay so if you want to create another domain name then you need to upgrade and give send some kind of money but for free I can use this one okay if I click on it and this is the API I want to use the API and if I click on select and these are the languages that is going to give me different tanks okay so if I click on node and this is how I can send the email to the respective recipients okay so for the free account of the mail done I need to add the authorized a recipient over the right side okay and here I can create different email ids where I want to send the email okay I want to send emails to any email ID then I need to upgrade my account okay and these are the email ids that I have added as a recipient and I have authorized these verified these emails I did from the email ID okay so you can see that this is my email ID and this is how I agreed upon that I want to receive emails from mailgun okay and this is the email ID this one okay so what I'm going to do is first of all I'm just going to copy these three lines first three lines and let's go and I'm going to paste it over here but before that I need to install this NPM package mailgun Gorge air in the terminal NPM I mailgun and I'm going to hit enter so it's going to download it from the NPM server and it's going to install this package in my current project if I go to package or JSON file then it should be added over here in the dependency alright so one thing I want to mention here is that there are two properties def dependency and dependency I needed to add known more norm on in the dev dependencies object okay but it's fine if it's added over here okay so now males and should be added you can see that this has been added over here and it should be installed in my Nolen dress code modules folder alright so I'm just going to close it okay so your domain name here I need to add a domain name over here okay so let me just go over here and this is the domain name that it has provided me so just copy it from here and I'm going to paste it like it okay so now I need to prove an API key okay so I can create this API T in my environment so let's go and I'm kind create it I can directly create it over there as well but this is a correct way of doing it so mailgun underscore api key okay equals two and it should provide us the API key so you can see that this is the API key and this is the API base URL and this is the URL that I have placed it just right now this is a URL this is the API key just copy it and paste it forward here okay I'm gonna close it so now I need to provide process dot C and B dot and I'm going to copy this name paste okay so now it has been pasted and this is the domain name that I have just added all right so now here I'm checking if the user already exists okay let me just remove it over here just bring it down okay so if user already exists and I'm going to show this message and if not then let me just try sending an email okay so just copy this data and paste it over here okay is it mg right yeah it is mg and here it is from the users so email from from which email iti need to send it so it can be anything like no reply ad hello calm it should be a correct format but we can write anything over here okay and for two we can actually use this email id that we have sent from here okay so email okay subject and text these things okay so let me just try it okay if it sends an email or not just leaving this thing over here okay so now let me run the server NPM drunk start and my server is running now and let's go to here and this is the email ID let me just add my other email id that I've just log n so this is the email ID okay I'm going to paste it this is the email this is email I'm going to place it over here okay so this similarity that I'm going to be irritating an email from email to and this is the name and this is a password so I'm going to click send so fine up success alright and it should be added in the database because I am writing this thing and let me go to my email let's go to Sam alright so it is bent into the spam actually so be careful with this message so we can just ignore it for now so this is a subject and this is a message that we have sent so eventually we have got an email from the mail done through our node server okay so nice all right from now let me just move forward and send an email with a proper token okay so for that I'm going to be using the JSON web token and if I go over here and this is a JSON web token and for that I'm going to be using this data name email password in order to sign add in different proper format okay so what I'm going to do is first of all I need to actually require this thing cost JWT equals to require JSON web token okay and here I can actually create a new token okay so cost token equals to JW t dot sign name email password okay and after that I need to provide a key and four key if I want to decode this token I need to use the same key okay so for that let's go ahead and I'm going to create a new activation g JW d acc activate okay and here I can write anything account activate T one two three okay so this is how I can write and the JWT ACC activate I'm just going to close it and after that I need to provide process toward env dot and the name of that environment variable okay so the last thing I need to provide is expires in and I want to give it 20 minutes 20 M okay so this is how I can create a new token using the data that is sent from the user okay and now I can for this token to a particular email account that this email ID okay so this is a subject subject account activation link all right and I'm not going to use the text instead I'm going to be using HTML and I'm going to use characters above tab button on your keyboard okay so here I'm going to create h2 please click on given link to activate your account okay and after that in the P tag I can actually pass the token okay so so zoom that on your browser on your email id you get the you get the URL and that URL should be added in your client-side application whether you are using view simple JavaScript angular react then you need to create a separate route on your front-end application that we can actually use over here okay so when we click on that link on the front and app then we need to navigate to that page and that page should show us activate button when we click on that button then we will actually pass a token to activate calm okay so I will show you that what do I mean by it so here process dot E and V dot R in env file let's say that I want to create a new this is a client URL and assume that we are using react and by default react for is 3000 so we can write localhost colon 3000 and this is the client is URL okay so it's up to you wanna use HTTP okay so I should he be local 3000 anyhow if your front end gets changed then you can need to change this as well alright so process dot E and V dot client URL for the email we'll be having this URL and after that I can have authentication slash activate okay and lastly I'm going to have this token okay that I've just created above alright so now I have sent this data let me just make it above and I have got this error so I am just using F error and I can return the response towards JSON message error dot message okay if there is no error then I can return response or JSON and here I can give it a message so let me just change it to error and for success we are going to give it the email id okay email have seen sent currently activate your account alright okay I'm just going to remove this thing because here I'm not going to create a user in my database at this position alright so that's it so let me save it and if I simply go to database refresh and the children has already been created but I just want to remove it because I don't want to return it from directly over here okay so let's go to it and let me click on send okay so that how it works I have got this message email has been sent kindly activate your account okay alright so I will be having my email in my spam folder so this is the spam so you can see that no reply account activation link okay so now you can see that this is the link that I have got I actually added it in my feedback so I had to add it in anchor tag in order to make it clickable okay you can do it yourself okay so this is a token that I've just created using JSON web token which I can pass from client-side web application to the backend and client-side should have this route to actually show you a page where you can click on a button but it's up to you what kinds of application you wanna use but in this video I will be using postman to actually pass is token to a specific route and see if the user account gets activated or not if it gets activated and I will be storing the user in my database okay so I've got this email perfect so now I'm going to create another route and in this route I'm going to call it router dot post and I'm going to name it email activate okay and this is a sign up I'm going to create activate account function that I haven't created as of now but I am just writing it over here it's going to throw me an error in the terminal job you can see it has shown me an error I'm just going to copy this thing and I'm just going to come back to my controller and here I can create that function okay so expose toward paste request respond all right okay so I've created this function there is no error in my terminal all right so you can see that our course is token so what we can do is from the client-side application also a postman we can pass this token in the body or in the header so in this video I'm going to be passing this token as a body okay what do I mean the body this is a body if I'm apposite who held it I can create a token T over here and I can get the value of token over here all right so I will pass it through a body and now let's go back over here and I'm just going to give it a token from the body which I can receive requested body and if dokkan exists that I'm going to proceed and else if token does not exist then respond so Jason and then I can give it an error something went wrong okay it's up to you whatever you want to show all right okay so here I can verify the token using the JWT verify command here I used signed and but I can use a verify function of it as well so JWT dot verify token and after that I can use the process dot C and V dot JWT ACC activate so for verifying this is the that I need to use same way in order to decode tack token from here okay so I'm going to use the same thing and after that I have the callback function error decoded token okay so if I caught error because of some reason I'm going to return a response data for double zero dot JSON and here I can pass the error so in correct or expired link okay so this is a message that I will be returning okay and if there is no error then I can proceed further and this is a decoded token and from this decoded token I can actually get back name email and password from tag token okay so let me just get back cost name email password and D coded token okay all right so God name email and password and here what I can do is I can actually check if this email exists in the database so what I'm gonna do is let me go up and copied this code okay this one code so let me just paste it over here let me uncomment okay so alright so here I am actually checking if this user exists in that database then I'm going to return a message user with this email ite already exists okay and then if not then I'm going to create a new user and I'm going to save this thing in the database error adder in sign up boil account activation ok and here it shows an error error activating account let me just error activating account so this is for redundant response for front-end application and this is for ourselves to see the response in the terminal this is really helpful for actually debugging the code ok sign up success alright so let me just save it and this is a route so I'm just going to copy it and here let me just go to the controller you can see that the previous email that I caught having this token created and it is going to expire in 20 minutes so if it expired then I should be getting this length so 20 minutes haven't passed as of now so I'm just going to use the same token that I've just sent so I can actually send the email again and try to sign up again if the token expires okay so just go over here and I'm going to create another request to add request activate account okay so this is the post and I'm going to pass it as URL slash API slash email activate let's save it okay and I'm going to pass the header so in the first thing if I go to the header bulk addict just copy it and come over here in the header click on bulk add it and paste it click on save that come to body raw Jason and here within the object and I'm going to pass the token because I'm going to receive the token in the body so this is where I'm going to pass a token let me go back to the email and I'm going to copy this thing let me just come back to the database so this is the user you user doesn't exist as of now okay so you can see it doesn't exist so I'm just going to completely token open the postman paste over here and let me just first change the token okay I should get an error that invalid or expired open so click on send okay I need to rerun the server so we have been actually using the same run command so whenever you change the dot Ian we file even though you are using node Mon then you need to actually restart your server using the same command okay so server is running ok and now let me just hit it why this might happen the server could not respond URL so this is the URL alright there is a mistake I need to add the double curly braces over here the URL for him click OK invalid or expired token okay because I remove the last character from here so if I go back last character was W so let me just add the W key word send and it says sign up success ok all right so our email has been activated now so if I go to the database refresh click on user and now you can see that our user has been created and I haven't added any kind of be Christian encryption for the password it's at this separate topic and I have already explained these things in my other videos okay so this video was only for activating an account so my email has been activated and the user has been created in the therapy okay so this is a code for actually activating an account so what you can do is this route you can use to sign up the user whenever users fill the form and this route will be called ok and then the email will be sent to the user and the user will get the link and that link should have been created on the front end which we can click and then we click on it the front-end route should show us and activate button ok and then we click on that activate button we are going to call this function and we are going to pass tech token from the URL ok now when we click on that link from the email we will have the token in the URL so we are going to get the token from URL for data to this function as a body ok to this particular API route and this will perform the account activation this is the whole logic ok so count will be created so if you want to sign in from the user then what you can do is you can actually perform the validations if the email exists in the database and you can actually show a message that a user with this email does not exist please sign in with another email id or prior gain and then what you can do is for signing in if user exists in the database that means it was already activated if it wasn't existing in the database and it was not activated so this is how user cannot signed in before it activates his account ok so let me just hit it again ok so it shows a message user with this email already exists so this is coming from here ok so I hope that you have liked my video pretty long video because I wanted to explain each and everything and in the next video I will be explaining you how to implement forget password or reset password functionality in your no DAC press back end and what what other things we need to care about in order to implement that so if you have liked my video make sure you subscribe my channel hit the like button and comment below if you have any questions thank you
Info
Channel: Awais Mirza
Views: 32,623
Rating: 4.7660379 out of 5
Keywords: node, express, email, activation, mongo, mongoose, learn node, learn express, api, rest api, mongodb, controller, model, route, sendgrid, nodejs, expressjs, node tutorial, email activation
Id: CEim3tZsp1Y
Channel Id: undefined
Length: 40min 31sec (2431 seconds)
Published: Mon Apr 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.