How to Create Login using JWT Token in PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends under this video tutorial we are diving into the world of secure authentication as we guide you through the process of creating a login system using JWT means Json web token in PHP in an era where data security is Paramount so we have to understand how to implement robust authentication mechanisms is crucial task so JWT provides a powerful solution for token-based authentication which offering efficiency and scalability whether you are a season developer and looking to enhance your skills or a beginner eager to grasp the fundamentals then this tutorial is designed for you we will take you the step by step through the process and explaining each concept along the way now let's start discussing this topic so this our testing database and under this we have one user table with table column like user ID user email user password and user name and under this table we have already insert one sample data for this tutorial and we will make login page and by using this data we will authenticate user by using JWT token now here we have open working folder in text editor and first we want to install library for JWT token so we have goes to command prompt and goes into our working directory and then after we have run this command so this command will start download and install this library now here we can see that PHP library for JWT has been downloaded and now we have goes to text editor here we can see that library has been downloaded under wender folder and here we have already created index welcome and logout file so first we have open index file and under this file we have already included bootstrap library now first we have to create login form under this file so here we have to write form tag with method attribute is equal to post under this form we have to create one text box for enter email address details so here we have to write input type is equal to email and after this we have to write name attribute is equal to email next we have to create another field for enter password details so here we have to write input type is equal to password and after this we have to write name attribute is equal to password so after creating login field now for submit login data we have to create login button so here we have to write input type is equal to submit and then after we have to write name attribute is equal to login and value is equal to login so here we can see that login form has been created now we have to proceed for authenticate user by using Json web token so here first we have to include composer autoload file for PHP Json web token class file after this we have include PHP names space Bas for this BP Json web token library next we have create $1 error variable with blank value and under this variable we will store validation error now for validate login form data so here we have to write if statement and under condition we have to write PHP is set function with dollar post login variable so this condition will check if this variable value is set then it will execute if block of code under this block first we want to make database connection so here we have to write dollar connect variable is equal to new PDO class object with three argument in first argument we have to write MySQL host is equal to Local Host database name is equal to testing next username argument we have to write root and in third password argument we have to write password so this code will make database connection so after making database connection now we want to validate user has enter correct email or not so for this here we have to write if statement and under condition we have to write PHP Mt function with dollar post email variable if this variable has blank value then it will execute if block of code and here we have to write dollar error variable is equal to error message like please enter email details after this we want to check that user has enter password details or not so for this here we have to write else if statement and under condition we have to write PHP Mt function and under this function we have to write dollar post password variable so if this variable has blank value then it will execute this block of code and here we have to write dollar error variable is equal to error message like please enter password but suppose all condition Falls then it will execute else block of code and here we want to validate user login details and if it is correct then we will generate Jon web token and authenticate that user so for this here we have to write dollar query variable is equal to select star from user table where user email is equal to question mark So this query will check user email data now below this query we have to write dollar statement variable is equal to Dollar connect variable with prepare method with dollar query method so it will prepare query for exit execution now we want to execute above query so here we have to write dollar statement variable with execute method and under this we have to write dollar post email variable so it will execute above query now for fetch data from query execution so here we have to write dollar data variable is equal to Dollar statement variable with fetch method and under this we have to write PDO class fetch associative array method so it will fetch data from query execution in an array format and store in dollar data variable now we want to check user has enter correct email or not so here we have to write if statement and under condition we have to write dollar data variable so if this variable has no value that means user has enter incorrect email and it will execute El's block of code and here we have to write dollar error variable is equal to error message like R email address but suppose user has enter correct email address then it will execute if block of code and under this block first we want to validate that user has enter correct password or not so for this here we have to write if statement and under condition we have to write dollar data user password variable value is equal to value of dollar post password variable if this condition Falls that means user has enter rowing password and it will execute else block of code and here we have to write dollar error variable is equal to error message like rowing password but suppose user has enter correct password then it will execute if block of code and under this block we want to generate Json web token and authenticate user so for this here we have to write dollar key variable is equal to this key so here this is Json web token key which it will use for encode and decode user data after this we have to write dollar token variable is equal to Json web token class with encode method so this method will converts and signs a PHP array into Json web token string in this method first argument we have to Define payload PHP array in Array first key we have to write issued at and in value we have to write PHP time function in second key we have to write not before and in value we have to write PHP time function in third key we have to write expired and inv value we have to write time function plus 3,600 so this token will expired after 1 hour in fourth key we have to write data and in value we have create another array and in this array first key we have to write user ID and in value we have to write dollar data user ID variable in second key we have to write user name and in inval we have to write dollar data usern name variable so in subarray we have to user data and in main array we have store Json web token time data in second argument we have to Define Json web token key so here we have to write dollar key variable and in third argument we have defined algorithm name which will be used for encode and decode string so this code will convert this array data into Json web tokens string so this way we can generate Json web token so after generating Json web token now we want to store in cookies so we can authenticate user data on another page so here we have to write set cookies function with seven argument so in first cookie name argument we have to write token in second argument value we have to write dollar token variable in third expire argument we have to write PHP time function plus 3600 so it will expire this cookies after 1 hour in fourth path argument we have to write slash in fifth domain argument we have to write blank value in sixth and seventh argument we have to write true and true accordingly so here we have store Json web tokens under cookies by using this function after this we want to redirect to welcome page so here we have to write PHP header function and under this we have to write location with welcome.php so it will redirect to this page after this we want to display validation error on web page so here we have to write if statement and under condition we have to write dollar error variable value is not equal to blank if this dollar error variable has some value then it will execute if block of code and here we have to write PHP Echo statement with this code which will display error message on web page now we have goes to welcome.php file and here also we have already included bootstrap library and some sample HTML code now under this file we want to authenticate user by using Json web token so here first we have included compose autoload file of PHP Json web token Library after this we have include Nam space of Json web token class and after this we have include key name space of Json web token class now we have to write dollar key variable is equal to Json web token key which we have already used in validation of user login data now we want to check user is login or not so for this here we have to write if statement and under condition we have to write PHP is set function with dollar cookie token variable if this variable value is not set then it will execute El's block of code and here we have to write PHP header function with location set to index.php file so if user is not login and try to access this page then it will be redirect to login page but suppose above condition true then it will execute if block of code and under this block we want to decode Json web token string so here we have to write dollar decoded variable is equal to Json web token class with decode method with two argument in first argument we have Define Json web token string which is store under dollar cookie token variable in second argument we have to Define Json web token key and algorithm details so here we have to write new key object with two argument like dollar key variable and algorithm name so this code will decode Json web token string for authenticate user and store data under this dollar decoded variable now we want to print login user name on web page so here we have to write PHP Echo statement with dollar decoded variable with data variable with username key which will print login usern name on web page which we have store under Json web token string after this we want to make log out link so here we have create one anchor tag with href attribute is equal to logout.php and in Anchor text we have to write log out so by click on this user will be log out from system and redirect to login page so for this we have goes to logout.php file and here we want to clear the Json web token cookie on the client side so for this here we have to write this set cookie function with this argument so this code will clear Json when token cookie data on client side next we want to redirect to login page so here we have to write PHP header function and under this we have to set location with index.php file so it will redirect to login page so here our code is ready now we have to check output in browser so friends here on web page we have open login on web page now first we want to check form validation is working or not so we have directly click on login button then it has display error message like email is required so we have enter email details and after this we have click on login button so now it has display password details is required so now we have entered Ru email address and Ru password details and after after this we have click on login button so after click on login button here we can see that ran email address error message has been display so now we have enter correct email address details and drawing password details has been enter and now we have click on login button so after click on login button here we can see that row password error message has been displayed on web page so here all validation are working prop l so now we have enter correct email and correct password details and after this we have click on login button so after click on login button here we can see that we have loging into system and we have been redirect to welcome page and here on welcome page we can see that login user name has been displayed on web page this username has been fetched from Json web token which we store after validate user login detail so here user authentication has been validated by using Json web token now for log out from system here we have to click on logout link so after click on log out link here we can see that user has been log out from system and redirect to login page now we have directly access welcome page without login into system so here we can see that user has been redirect to login page if you user directly access welcome page without login into system so this way we can Implement Json web token library in PHP for authenticate user so if you have any query or input regarding this video so you can write your query in comment box and if you like this video so subscribe this channel for update of upcoming video tutorial
Info
Channel: Webslesson
Views: 3,677
Rating: undefined out of 5
Keywords: jwt, jwt token, jwt authentication, jwt login, jwt login php, jwt authentication php, jwt php example, php jwt authentication example, what is jwt authentication, php login jwt, php jwt login, php jwt login tutorial, json web token, Web Development, PHP JWT Tutorial, Token-Based Authentication, login using jwt token, login using jwt token in php, login with jwt token, PHP JWT Library, Firebase php-jwt, jwt php login example, jwt authorization, webslesson, JWT Authentication
Id: JF9IqF77OxI
Channel Id: undefined
Length: 19min 54sec (1194 seconds)
Published: Tue Jan 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.