Laravel Passport Authentication | OAuth2 Authentication | Token Based Authentication | API Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends welcome back to api tutorial with laravel 8 today we will going to learn about laravel passport authentication last day we discussed about laravel sanctum basically this was also a authentication technique mainly for a single page application or mobile token or api token purpose okay so if your application is absolutely needs a support of oauth 2 then you should learn laravel passport okay so first we need to know what is oauth 2 basically oauth is an open standard authorization protocol or you can say framework that describes how unrelated servers and service can safely allow authenticated access to their assets without actually sharing the login credential okay here client sending a request to the resource owner to grant the access and resource owner is the only app or you can say application or medium that can authenticate you then the response comes authorization server through the client and it generates the access token okay so basically this is checking the authorization and granting your request and finally this token is using to access your application okay so this is the resource server and finally it is accessing so this is the oauth 2 flow and jlr passport follow this rule okay so let's install passport to your application first okay so to install go to the terminal so here is my project that is laravel api tutorial and install laravel passport with the composer so right here composer require and use the laravel slash passport okay so just enter this so this will take some time to install the level passport in your application so when passport is requesting to your application it is creating its own database migration directory with own framework so we need to migrate our database after the installation so basically this is installing the 10.1 version of learnable passport and its composition has been updated and it's updating all the dependencies required to your application so laravel passport installed in our application and the package manifest generated successfully so there is also generate is a migration file um and going to migrate those file so just before that go to the database and check so this is my database table and here is my users table so basically if we check our project directory in the database section and there is a migration so here so some migrations are generated so let's migrate this first so to migrate we to use php artisan migrate okay so the create auth access token table and there is another table this is access token tables and refresh token tables and there is client table so basically this four five tables are created and so go to the database and refresh so we have installed all the table related to passport so all the database is ready and the package manifested generated successfully so now we need to install the passport in your application so write php partition passport okay and colon install okay so using this command the password will install in your application so the client one that is secret key generated the password grant client created and the second client id 2 the client secret id also generated so passport is installed in your application so basically this will create the encryption key to generate a secure access token and also it will create a personal access and password grant clients okay so which will be used to generate access tokens okay so now i want to use the passport in our user model so basically user model is the by default model we will get when you install the laravel so just go to the app and inside the models we have the user model so here we will add the passport rate into our user model so i just write use larabel slash passport slash hash api token okay and use this token for user model so just right here and comma okay so now we need to call the passport from our boot so just go to the app and inside the app we will get the providers so there is all service provider so write use larabee slash passport slash passport okay and let's call inside the boot so right here the passport double colon routes okay now our all service provider is set so now we're going to change the api driver so just go to the config so here you will get the config and there is auth dot php so here we will use the guard so basically we are using the api we need to write the driver name equal to passport okay and and we don't need this so we just close this so now i will create a controller and will use this authentication from there okay so suppose the model is user so i am creating the controller that is user controller so write php artisan make controller suppose my controller name is user controller okay so we just enter this so controller created successfully so just go to the project directory and replace this so http controllers and we'll get the user controller okay so just use the authentication so first we'll include illuminate slash support slash facets slash auth okay and we will access the user model so use app models and schneider the model is user okay and we'll use the validator also so right here the validator so validator okay so here we will also check if the request is valid then only will process otherwise it will go through to the unauthorized page okay so basically the model is the user model so go to this model so i am using the app models and user okay so here we will create two function basically one for restation and one for login okay so just create two function that is public function first we'll add registration okay and next we'll add the login function okay public function login okay and we'll call these two functions from the routes so basically here we will use the api routes for api related purpose so go to the routes and call the ap dot php so first which include the controller name space so basically the controller trades so just writes the app http controllers and name of the controller that is user controller okay so uv is caps user controller okay and we'll call these two functions from this robot so just create two function that is route colon gate sorry it will be post for restation purpose so here we'll write the register and and the controller name is user controller so we just copy from here and then colon colon class and after that name of the method so the method name is registration okay and we will call another routes for login so just write login and the method name is login okay so i want to create another route for login and it will be the gate purpose because when the unauthorized user will going to access so basically it will redirect to the main login page which are this login okay so route is set now write the registration functions okay so basically it will use the request as dollar request and here we'll call the validator first because we are using the better class so we'll first check the validation so i am taking a validation variable and inside the validation variable will call the validator class okay and she will make to make the validation okay so i i'll create a array so first we will take all the request that is dollar request arrow all and will valid all those things from this request so here i will add the validation for users table that is name email and password okay so right here name and name is required and same like email email is also required but email is should be email type and that means valid email and the password the password is also required and i will use the confirm password that is c password okay so basically we will validate the main password and confirm password it should be same so we need to write another pipe and use the same as the password okay so my validation rule is added and now i will check if the validation fails okay so dollar validation fails then it will write a error or it will throw a simple validation message okay so just write the return response and we'll pass the json response that is dollar validation and we'll throw the errors okay so right here errors and after that we'll send the error code that is 202 okay and if all are okay then we'll take this into the variable that is all later okay and we'll take all request that is request all okay and after that we will insert into the user table so basically we will use bigquery encryption for password so just use the all data password akl2 be crypt that is b c r y p t b crypt and the all letter password okay so i'm just changing the password into the database format okay and others are okay so just take the user model so i am taking a variable for user model that is user and calling the user model that is user model and we'll call the create method okay and we'll pass all the data okay after that we will create a response array so that is race array so basically this is the array and using this third packet will initialize the array so use this response array to generate a token and we'll use this for our future purpose so just recent array and we will click a token so basically i'm create token so how we can create token so just call the user and create token okay so basically create token method will generate a token suppose i am creating any things that is api application okay and after that the access token okay ascce ssx is token right so now i need to pass the username into this so just write user array and write the name so basically the name is in the user so user arrow name okay and just written the response okay so right here return response arrow and it will pass the json type response okay so just pass the response array and the response code is 200 okay so registration process is complete and i will create another function for login so write the login function so the login function is simple so just write request dollar request okay and we'll write if the auth attempt is true means we'll write the auth colon colon attempt that is attempt will use to login to the laravel application and inside the attempt we will take the email and password which will get from the passport so i'll create a array so instead array will write email so basically email will get from the request so we'll write the request email and we'll check another that is password okay so basically for login we will use the email and password so right here request password okay so now if the user equal to auth column colon user then we'll create a response array like previous so write response array and just copy from the previous so here write these things so response area that is responsible token risky token and the name and after that written okay so just paste it here return response all those things and if the authentication fails then else write the unauthorized okay so just copy from here and write the error message okay so write a json array that is error and we'll write unauthorization or you can say unauthorized access and pass the status code to 0 3 okay so my login and registration functions is ready so now go to the postman and check so here is my postman so i am creating a request so first we will register so we'll create the post type okay and here is the url that is 127.0.0.1 colon800 and after that the api so after that we will use this okay so we'll use the register and here we will use the validation so just click on send and it will response that is the name field is required password is required and confirm password request so why not email variation showing so just go to the function show sorry here the name should be changed so this will be the email so now click on the send so the name fill is required email is required password require and c password is required okay so just write the name so just go to the body and go to the raw data and use the json okay so we'll pass the json array from here so here i will write the name suppose name is john and we'll pass the email so email is johnathemail.com john gmail.com and password so basically password is one two three four five six seven and the confirm password should be same so the confirm password is c password okay so just click on the send button so it will register a user name john gmail.com okay so click on the send and sorry there is a error in the user model and number 13 so about the user model so here the else would be in caps and here it will be the tokens and this will be the same as here okay so now just go to the postman and click on the send okay so we can see the token is generated okay and if you go to the database we can see the one user is created so this is the user created and if you go to the database and we can see the oauth personal access clients so there is a two records so basically this record also created okay so now if we want to login so what happened so just go to the postman again and create another url for login so right here login and create a request that is request and we will create the post so we will log in with the username and password so use the json formatted data so here my login credential is just copy from here and to remove the name and pass only the use email and password okay so click on the send we can see sorry the url should be changed that will be the login okay and now click on send so here the token is generated and the name is passed with this array so basically the name is coming from this user controller this section actually we are passing the name okay so now the question is how to authorize your url okay so in our last video we created some links that is the blog and there is the menu links inside there so first we will use this link so just go to the url and create another link that is the gate and we'll use the link that is 127.001 and the api base url and the link is the get blog okay so if you click on the gate block we will getting some records from the block table okay so now i want to authorize this means if the token is valid or if the user logged in then only it can be see so we need to create a middleware so just go to the routes so here we will create a middleware and write route middleware and here we'll write the auth api okay and we'll call the gate method okay so just copy from and write this okay so now it is not accessible without the authentication so first close this url and write it and refresh the postman so if you click on the send button again we can see the unauthorized access that is route login not defined that means it will searching to login okay so now to access this url we need to authorized so recently i logged in and this token is generated so if we pass this token we can access this again so right here that is pass in the head the authorization so click on the barrier token and pass this token okay so now if we click the send button we can access the block okay so we can understand how we can do this so if we add all the routes or if we add any routes which i want to authorize use this in the auth api middleware and we can authorized our url okay so that is all about laravel passport authentication so basically this is the oauth authentication so if you have any queries about this video please comment in the below comment section so basically laravel passport is easy and you need to follow some steps to achieve this okay so that's all about laravel passport so next day we will going to discuss about some another topic with based on laravel or laravel ap related so please like and subscribe this channel for upcoming video thanks for watching this video bye
Info
Channel: Learning Points
Views: 7,836
Rating: undefined out of 5
Keywords: Laravel Passport, User Authentication with API request, How to pass Bearer Token with API request, Token based authentication with laravel, What is OAuth2, Laravel Passport Authentication, How OAuth2 works, How to register user using passport, How to login user using passport?, OAuth2 Authentication, Token Based Authentication, API login and registration with Laravel Passport, API Tutorial, Laravel 8, Learning Points, api tutorial, laravel tutorial, laravel full course
Id: j-gF5Qwowy4
Channel Id: undefined
Length: 25min 37sec (1537 seconds)
Published: Wed Jan 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.