Confirmation Email with Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys I'm sure you've seen lots of different websites when you register for an application they send you an email and you have to click on a link and before you click on the link and confirm it you can't access their website they require you to click on the link and the reason for that is to avoid spam and people trying to put fake emails and that sort of thing so today I'm going to show you how to do that exact thing using no GS now we're gonna be using a library called node mailer to do this you could switch this out with a different service like SendGrid or AWS has sending emails the same strategy will work for all of those but first let's show an example of how this works so I'm about to register for an account I have a temporary email right here I'm gonna use to demonstrate this so I'm just gonna copy this email here and I'm gonna register using that so I'm just gonna use that for the username email and password I'm go ahead and register and as you can see it takes a second to register and then we will see registration worked and the reason why it took a little longer to send there or to get the response back it was one or two seconds is because we're actually sending an email before we're doing the response and then if I were to come over here and I try to login paste this here we get a please confirm your email to login so we can't login yet because we haven't confirmed or email so I'll come over here and we see we got an email here from bin Awad Apps and it says please click this email to confirm so I'm gonna go ahead and click on this link and I take it to the login page and I'm gonna console ok click on that and we put it in and log in worked so cool we successfully confirmed our account and are able to log in now of course you could do different splash screens in the middle there and say hey you just confirm doing email click this button to login now or what not I just was redirecting them straight to login so let's I'll show you next how we actually got to send this link how this link is working in the code kindness the power this guy because this is pretty cool so let's come over here this is the code for it the first thing I did is I made a confirmed attribute in the user table or property so now I know whether the user has confirmed their account or not and by default that is false and then whenever I am logging in the user I'll just check user dot confirmed and if they are not confirmed I'll say please confirm your email to login so that's kind of what I'm doing to check and make sure that the user is logged in now to send the email I told you I was using node mailer and that's what this register function is doing right here so it's first creating a user right then it's using node mailer and we created a transport I'll show you that in a second we're creating a transport and we're also passing an email secret because if you look at this closely over here this is actually a JWT token I'm passing back so you can use it for multiple things and one of them is email confirmations it's really nice so we're signing that token with an email secret and the transporters actually send the email now there's two different types of emails that I'm going to show you guys of ways you can send the email I prefer the async email because it's faster so you saw when I clicked register it took like a good 1 or 2 seconds to get a response from the server and the reason for that is we commented out async email and right here we were doing synchronous email and the way synchronous or how this is working is I'm actually creating the token this is the token that I'm going to send in the email and I'm all I'm doing here is I'm passing the ID of the user so I know when the user I basically know which user to confirm so we're storing the user's ID here and we're using the email secret and I'm saying this expires in a day he could do a week however long you want your basically link to be valid for then we're linking to this confirmation so this is the Express server localhost 3000 slash confirmation passing that token I'll show you guys how this route works in a second but that's the URL we redirect people to and then we're doing transporter dot send mail and we're awaiting the response so this is what's taking a long time because we're actually waiting for the mail to get sent and make sure and if not we're catching any areas that occur and we could like tell the user hey the email wasn't sent for some reason but a lot of times you don't see that a lot of web sites what they'll do is hey did you not receive your email click the link or something right to resend it because they're making their email sending asynchronous because you saw how long it takes as you can see here's our actual email the subject is confirm email and then the HTML all I'm doing is putting a link in there and doing the URL that we created right here so let me show you guys though how much quicker it is if I do this one so this is an async email I'll show you guys how much faster this one is so let's come over here I actually can't use this same email actually I'll do is I'll just drop the database real quick so I'm gonna come over here to index and I'm gonna say force that's true so this will drop the database and let me to recreate this user so I can show you guys just how much faster this register is so go ahead and put all these fields in and we hit register and look how much faster the responses but it'll still send the email for us it just sends the email and a different thread it's asynchronous so if we wait here a little bit longer we would see probably another message pop up I'm not sure if you're gonna see the same I don't know how how this truncates messages since it's the exact same message I don't know if it's gonna get rid of it or what but okay we don't have to worry about that but you guys saw how much faster so with the sync async one what I'm doing is I'm signing the token and they can't give you a callback function and in that callback function I am the exact same thing I'm creating the URL and then I'm sending the email in the same way the only difference as you see I'm not awaiting the response so this is happening asynchronously okay let's take a look at what this end point right here is doing so you see how we're signing the token with the ID and we're saying it expires after a day and then we're using this email secret this is important and then we're sending them to click on that link so when they click on that link what's gonna happen is they're gonna come to this endpoint up here this is has nothing to do with graph QL right now this is just plain old rest endpoint confirmation expecting a get route and we're accepting a token as one of the parameters in the URL so you saw here how I was doing slash and then I'm passing my token I'm reading in that token right here and that's what wrecked up RAM slot tokens gonna be is whatever they pass here so I'm just doing a try-catch here I first try to verify the token using that secret that I have that's why I know no one tampers with the token and changes it in any way and make sure it hasn't expired it's been passed a day we don't want to verify them or whatever long so we grab that ID that we had and all we're doing is we're then with our sequel eyes model I'm updating that user so where the ID is equal to the ID we stored in that token we changed them to confirmed if something goes wrong we just do res dot send air and then I'm redirecting if everything worked to the front end and I'm just doing slash logins so they can log in so that's it guys that's how it works so as you can see it's actually not too bad to do email confirmation and you can switch out this portion of the code right here in the resolver so in the resolver we're sending an email with this transporter using node mailer but you could do something else besides node mailer and I was going to show you guys the transporter that's up here here's the transporter this is me carrying node mailer and I'm using my gmail account using my gmail username and password I'm storing these environment variables so I don't show you guys my credentials but you could store these as strings if you wanted to and then yeah we're just confirming the token that we created in our resolve Rove here make sure nothing has been tampered with and then we're updating that user so pretty cool and let's see sure enough we get the second confirmation email here so it did work using the async method so I would recommend you guys doing this and what you could even do is have like a little length the user could click on if the email doesn't get sent or something that they can then do this so that's it for this video guys this code will be up on github so you can check it out and do confirm e confirmation emails yourself and the link will be in the description below so thank you guys for watching and let me know if you have any questions about this you
Info
Channel: Ben Awad
Views: 131,812
Rating: undefined out of 5
Keywords: confirmation email, Confirmation Email with Node.js, node js, node.js, nodejs, email node.js, email node js, email nodejs, confirmation email node.js, confirmation email nodejs
Id: 76tKpVbjhu8
Channel Id: undefined
Length: 9min 49sec (589 seconds)
Published: Sun Sep 10 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.