Create a User Registration System with Email Verification - Laravel (2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys is caller awesome here and today we gonna implement a sign up system with email verification so when the user signs up the website automatically sends an email to the given email address with the verification code so grab a coffee n let's get started for this we're gonna follow the common practice that most websites do when the user signs up with his details the websites automatically sends an email to the given email address with the verification code the verification code of course attached to a link like this as a query parameter so when the user clicks that link he is again taken to that website so the website can grab that query parameter and the verification code it can validate that verification code and map the user as a correct user as a valid user so that's what happening under the hood right so we're gonna implement this in our project open up you laravel project and open up the terminal i'm using the terminal comes with the IDE but you can always use the default terminal comes with your operating system right first I'm going to install a package I'm using this old package to save time but you can always use your own login functionality I'm going to install or package I'm going to install the larval UI compose this will install the laravel UI it will take some time and after the installation is done I'm going to type PHP artisan UI view yes - all right now the auth package has been installed I'm gonna open up the register controller that was installed with auth package and as you can see this create method is the one that creating users when they assign it up I'm gonna simply delete this method and create my own login functionality I'm gonna create a new function called public function register right this is where I implement my customize my own register functionality actually what we are doing here is as you can see this register controller use a parent class call register users if you click on this it will open up the parent class and you can see there is a method call register so what we are doing here is we are gonna override that register method with our own login functionality that's what we are doing so let's continue before writing the register functionality I'm gonna open up the users table migration that was created by default in the migration folder I'm going to open that up and add two more columns to this migration it will be a string column and the name is verification code and this field I'm gonna say to nullable and I'm going to create another column it is as an integer column I'm going to name that is verified and the default value should be zero right so they make sense to you we're gonna store the verification code then mark the user as a verified user right close this and run the migration PHP artisan migrate right then I can write my logging functionality I'm gonna create a new user instance this user model was created by default and I'm gonna put the name column like this and the email column then the password column I'm gonna use the hash function of laravel oops make and inside that I'm gonna request put request and password right and for the verification code I'm gonna use another hashing function call sha-1 and I'm gonna put the timestamp which happens it technically happens once so it is a unique for each use it right I'm gonna save this like this alright then I'm gonna check if the user is created is not null and if it is not null I'm going to send the mail in like this it's an email and redirect to the friedrich - oh so a message so user no I'm going to show some message to the user we have sent an email with the verification link like that or if the user is not created oh by some errors or something then I'm going to redirect redirect or message so error message so this is what we are going to implement next thing I'm gonna do is implementing mailing functionality for this I'm gonna use laravel mailable class I have a complete tutorial for this using this mailable class and creating implementing this mail functionality so I'm not gonna talk about it in detail but I will create the main level class from scratch here as well you can follow along first I'm gonna type PHP artisan make mail and the name of the mailable class is sign up email tender this will create the mailable class inside the mail folder here here we are going to create a new template for our email our confirmation email I'm going to create a new folder called mail so that I can put my own email templates here and create the blade call signup email that blade dot php' so this is my email template I can put whatever I want but I'm gonna go with a simple text base email content like this hello welcome to my website please lick the please click the following link and verify your account like this all right I'm gonna hit save and I'm gonna use it here sign up oops mail folder sign up email like this and I'm gonna pass some data from the constructor like this and email data equals to the data via passing right and I'm going to pass this data to the email template right for this I'm going to put some configuration as well I'm gonna use from address and from name and the subject you can follow this tutorial for complete guidance of implementing this mail functionality and I'm going to put my sender's email which is I have define in my environment variable file my env file it will be like mail underscore username and the name of the sender will be like my syama website's name oops it is code Rossum like this and the subject should be like I'm going to hard code it here welcome to go DeRozan right and we are going to call this from other register controller I'm going to create a separate controller for sending emails so that I can use that separately for implementing all the email functionality I'm gonna create a new controller PHP artisan make controller male control it like this I'm gonna close these two and in the male controller I'm gonna create a new method public static function this will be a static function so that I can call it easily public static function this will be like I'm going to name this send signup email and now I'm gonna call that Mirabal class we created fumes ago male the to address which is the recipients address and then the same method in the same method I'm gonna pass the mailable class we just created as the argument will be sign up female by the way this IDE imports all the classes and facades when I am typing so if you ID doesn't do that you have imported manually like this right now I'm gonna pass the user's name and the email and the verification code like this so the email will be added to this so that's the recipients email so it is the given email by the user and I'm gonna put the other variables into an array like this name and verification code like this so this array is passed to the available class we created like this so I can call this in sign up email from my register controller that's what we are going to do now so I'm gonna call this method here male controller method is sign up email great I'm gonna pass the email the name and the email and the verification code that is created when the user is created verification code great like this when the mail is sent I'm gonna show some message to the user for that I'm gonna redirect him back to the signup page redirect Beck with some message it will be like with here I'm gonna put some session messages so session Flash the message I'm gonna put the clasp like alert success and the message is your account has been created please check email for verification the link yeah whatever so that's the success message and for the error message I'm gonna change the class to alert danger these are just bootstrap classes that comes with level the message is like you know something went wrong right oops I made a terrible mistake here this should be not equal to null so if the user is created it is not equal to null then the maze should male should be sent and the user must be redirect back to the signup page right and this is almost ready and we have one thing left that we need to display this message in the signup page so currently there is no space to display that message I'm going to open up the log in register page in the both folder this in this blade file I'm gonna put some code snippet to display the error message you can use your own way of displaying that I'm using some some fancy way of displaying the error message in the session anyway I am gonna put this in the video description you can copy it and use it in your project right I'm gonna hit save and close this to babies and I'm gonna open up the browser and test this I'm gonna fill up this form with my name started block and one of my working email address and some crazy password then heat register button it's loading takes some time which means we will have a success message yes we do your account has been created and let's open up my email and yes we have received an email I'm gonna open up and hello yeah that's the content we wrote in our email template next thing I'm gonna do is I'm going to customize this email template currently it since it just the same email to a URI use it but I'm gonna customize this I'm gonna personalize this for each user hello with the recipients name and and then the verification code that is unique for each user so let's do it right now in the mail controller we are passing data like this to the mail mailable class we created name and the verification code so in the available class we pass this data to the email template we have created so this data can be used in the email template ok let's use the name of the user next to the hello so it will be like email data and named property like this and then the verification link HTTP let's say my website address is like this my website.com slash then the verification route which we don't have yet but we'll be creating it in a minute it should be like verify then the query parameter query manometer I'm gonna put is code and then the email data array and the verification code property like this let's test this on the browser and make sure you delete the user you have already created in our previous test the name should be telling the lock Sita and the email address is one of my working email address the same email address I used previously then some password hit register button it taking times which means it's gonna work and it is let us take the email yes we have a new email and I'm gonna open it up right hello starting the last year which is my name and our verification link it is working as it's supposed to because when I am hovering over with my mouse pointer you can see the website address and the route and the Cauvery parameter is there so once I click this did I'll be I should be taken I should be taken to the website and that particular out so currently I have put a different domain which should put my local host so we will customize that now my project you are like this I'm gonna replace this domain with my local project URL it is like this and this verify verify route which does not exist yet let's create it now I'm gonna open up the routes folder in the web dot PHP I'm going to create a new get route route get the URL segment is like verify and the register controller it is inside the folder and verify user we will create this method in a minute and name this route as verify user right let's create that method really quick in the register controller public function verify user in order to access the query parameter I am gonna need request class as the parameter I need to access the query parameter so I'm gonna create a new variable for that it is the verification code equals to I'm using request facade for that and gate so this way I can access the query parameter the query parameter we used was code alright once we grab the query parameter which is the verification code we need to search through the users table using that verification code so I'll be searching users like this in the user model I'm gonna use a vehicle here the column name is verification code excuse me I'm gonna use the first method because I'm gonna need just the first user because there is only one user with that verification code then if the user is found not equals to null then I'm gonna mark this user as a verified user is verified equals to 1 then I'm gonna say that user like this this is just a basic update Cory if the user is verified then we need to take him to the login page let's put it like this redirect route login the message should be like your account is verified please login great and if somehow it cannot find the verification code in the users table we need to redirect user maybe to the register out of the login route with the message that invalid verification code let's say the message something went wrong oh no really invalid verification code great now let's test this again open up the browser and make sure you delete the earlier user in the database yeah I have already deleted let's create the user with the same email address used earlier my name and my working email some password oops heat register let's see yes the count is created let's go to the email and we have a new email and yes we have our details and the verification code and see when you are hovering the URL is the local URL of my project now in the database the user is created with the details email address and password and all and the verification code now the is verified column is set to zero let's click the link and we'll see whether it is working I'm going to click this link and it is opening a new tab yes we are redirect back to the login page but we don't have a message that's because we didn't put the message space in the login page we'll fix that later and I'm going to check the database and refresh it and yes then the is verified value is now 1 so the user was marked as a verified user so it's working all right let's fix that message really quick in the log in blade I'll do the same I let the same message space to the login plate as well like this now I will test this later and one important thing we forgot to implement was the user is still can login to the system even if he is not verified we need to fix that first let's open up the login controller here we need to define a function like this credentials like this we are naming this function credential is because the parent class also contains a method call credentials so in that method it is only using password so we are in our login controller via overriding that function to use each verify method as well so I'm gonna return this return I'm gonna open up the open up this authenticate users class and copy this line of code that is already written in the player and method I'm gonna return this here and here we need to check whether the yeast verified column is set to 1 not 0 so this returns an array and I wanna return another array like this is fine the value should be one and we need to match these two areas we will use PHP or image function right now these tools to erase are merged and username password and is verified all these values are checked when the user is logging okay now let's create a user and let's try to login without verifying I'm gonna log in again and make sure you delete the earlier user my name and the working email of mine and some password I'm gonna hit register yes the it is working and we get an email as well now before verifying the account I'm gonna try logging in and yes I'm gonna hit login button and yes I get this same common error these credentials do not match our records so it's it's too obvious make sense because even though the user is created he is not a varied user in the system so the system can say yes we don't have such user until you verify you account so let's verify the account and try again I'm gonna click here click the verify link I'm gonna use my credentials and try logging in again yes now we are logged in so guys it's working as it's supposed to the system can register users and it can also automatically send emails with the verification link and when the user clicks the verification links it can validate the user and mark him as a verified user and also the system does not allow unverified users to login so the system is complete and that's it for this tutorial so I hope you guys enjoyed this tutorial hit the subscribe button if you haven't already and hit the like button as well if you have any concerns please let me know in the comment section and catch you guys in the next part thank you
Info
Channel: coder aweso.me
Views: 29,628
Rating: undefined out of 5
Keywords: Laravel, User Registration, Email Verification, Laravel User Registration, User Registration system with Email Verification, Laravel User Registration with Email Verification, Customize Laravel Auth Register, Send verification code, Verify user email address, coderaweoso.me, coder awesome, awesome coder, Laravel Login System, Laravel Verify User email
Id: sKDz7zcMVMg
Channel Id: undefined
Length: 38min 59sec (2339 seconds)
Published: Fri May 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.