🔥🤩 Step-by-Step Laravel 10 REST API Using Passport Authentication | Laravel API Development Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Hello friends welcome to all tutor I am sanj so this is really exciting to know that inside this complete video we'll see a complete concept to create rest apis using lavel version 10 and passport authentication right now if you have any hesitation to create restful web services using lavel and password so I'm 100% % sure after watching this complete video without any es skipe you will get the complete confidence to create rest apis using lavel and password authentication we have one more request please subscribe to this channel so that we can create such type of amazing quality contents for free for you if I back to slide so as I told you that this is a step-by-step course so here we'll discuss from installation up to API development process processes so this is all about complete playlist pan so first we'll see all about lavel installation after installation we'll then connect with the database after database connectivity then we will create a API controller class and inside that controller class we'll create our API needed methods methods as for registration API login API profile API and logout API so after creating API controller class then we go and discuss all about passport authentication package and settings then we create all about application API routes these routes are very important because by the help of these routes we'll call all about these methods what we create inside our API controller class so overall inside this whole course we'll see all about the concept of registration API login API profile API and logout API all about the API development processes we'll see from installation all about the standard apis and all the settings all the configurations of passwort will discuss into a complete detailed way inside this video so first we'll see all about the first step of this playlist that is all about Lael installation so let's get it started now to install Lael I will go to its official website lv.com here I will click on documentation it will be direct into the documentation of installation page and here I will search for composer because I will prefer composer installation here we can see here we have a command here I will copy go to slide open a new tab if I paste it here so this is all about the complete Command to install LEL using composer so if I see all about the command here composer space create hyen project Space level for/ larel space example hyphen app so this is all about we are trying to create a project using composer of lavel and this is our setup name so when we install using this command it will create a setup with name called example hyphen app so instead this dummy name let's pass a realistic name something called LEL hyphen passwort hyphen apis so I will copy the command here go to Project terminal so I will paste it here and if I press enter it will create a setup with this name into this folder let's press enter now we can see that installation is started successfully we have installed our setup into our local system so if I go into folder structure so this is the setup what we have downloaded so what we can do here I will open this setup into my vs code editor in your case you can choose your favorite editor so what I will do I will go right click open with other application I will select Visual Studio code and I will click on select button so successfully I have opened our setup into my vs code editor so first thing first I will try to execute this application into browser so to start development server I will go inside proac terminal and let's go in inside our setup so here we have the setup name as lavel hyphen password hyphen apis so CD LEL hyphen passport hyphen apis after going into project directory if I type PHP aition press enter it will open a command list so to start development server let's go up and here inside this available command setion we'll see a command call serve look at single L description when we execute this command call serve it start BP development server so how to start simple BP artian serve press enter now we can see that our development server started at local host and 8,000 P so to access this application simple we need to open this URL into our browser so I go and copy link go to browser open a new tab pasting it here and now this is all about our landing page of LEL version 10 so successfully we have installed our setup into our local system let's go to our checklist so successfully the first point of this playlist now completed in the next we need to create a database and connect with the application so first we to create a database so I will go inside PS admin click on databases here let's say level underscore passwort _ apis underscore YT so I will copy the DB name click on create button so successfully database created I will go inside proect editor so to connect with the database with this application I will openmv file which stands for envirment variable files I will go and search for DB connection and all these variables so here inside these variables as we are using Local Host so this IP is okay inside database name this is what we had created in my case the username of BMI admin is admin and password is admin at 1 to3 maybe in your case it will be different now successfully the created database now connected with this app application let's close this file go to browser reload this page so successfully now this application is connected with this database go to checklist so here the second step of this playlist also completed now next we need to create API controller class and inside that controller class we need to create API needed methods and one more thing before going further if you really want to learn all about the API development process of lavel and passport authentication please stay with us without any Escape otherwise you will go into line of Errors now in The Next Step let's create API controller class and inside that API controller class we create all these methods so as we know that inside lavel there are several options to create controllers inside our controllers folder like normal controller resource controller so here inside this this course we'll create a normal controller so that we can create all about our custom methods so to create a normal controller class I will prefer PHP addition command so before creating controller if I go to folder structure so as you know that whenever we create any controller class by default it goes inside app HTTP and controllers folder so what we want we want to create a API controller class right here in inside a surf folder called API and inside that API surf folder we want our own API controller class so what we can do here let's go to Project terminal I will first close this development server BP aition if I press enter scroll up if we go inside this make section so here we have a command called make controller look at here create a new controller class so we will use this make controller command want to create a normal controller so here PHP make controller as we know that we want a APS sub folder inside controller folder so first pass the folder name so we want this API inside this controller folder and inside that API sub folder we want API controller class so this is our controller class name press enter now we can see the that this controller class now created inside HTTP controllers and API Sur folder let's go to Project editor I will go inside app HTTP controllers API and here we can see our API normal controller class now after getting this controller class what we want we want to create our API needed methods for registration API login API profile API logout API all these methods right here inside this controller class so first I will go and create for register API so this API will hit using post request so the method syntax will be public function and register while calling this registration method we want to pass all about user data and inside user data we have something like name email and password so to get access all about our request part parameters we'll use request object so here inside this register method I will pass request class and we'll use request object now let's create another method and this is for login API this method will hit also using post request type so public function and I will call it as login while calling this login method we will pass email address and password so to get access all about the request body parameters here we want to pass request class and it's object now next I will create another method and that will be for profile API this method will hit using get request type so public function let's call it as profile and finally we need four log out API and this method will hit using get request types so public function and log out one more thing so here we have four different methods inside this API controller class where these two methods are open it means that while calling these methods we don't need any login or any authorization token value but while calling this profile API method and log out API method we want user must be logged in and also we want a authorized token value so these methods like profile and log out are protected methods so successfully we have created API controller class and also created our API needed methods so successfully from this playlist plan this third step also completed next we'll see all about the concept of passport authentication installation inside our setup and its settings so before starting all about the discussion and settings of passport first thing first what is passport authentication so passport term is not the simplist card which we usually use for tools and travels this is all to authentication surveys inside LEL it's a composer package by using composer we'll install this package into our setup and then we can use all about it services so to get started all about installation I'll go to browser go to this blogging website online blog.com and here inside this search bar I will search for LEL password once we type press enter it will go and search and pick an article here we can see L version 10 restful apis with passport Authentication so I will go and click on that article scroll down so here we can see that how to set up passport authentication inside Lael so we need to follow each step to install and configure all about passport authentication right here inside our setup so I will go inside this article so step number one as I told you that it's a composer package so we want to install first this package it means L for/ password using composer inside our project setup so in the first step I will copy this composer command go inside Pro terminal let's paste it here and one more thing before pressing enter if I go and let's close this first I will open composer. Json and inside this require object we can see that we don't have any level for/ passport package but once we install this from this composer command it will automatically add a line of code right here inside this requireed object so let's close that first and press enter in a moment as we can see it started installation now successfully it has installed if I go here and again I will open composer to Json now you can see that inside this Regaro object here we have l/ password if if I go into article so first step successfully now completed in the second step we need to run PHP artian migrate command this is a well-known command inside level by the help of that we execute migrations to create tables inside database so before creating tables if I go inside our project setup first I will go inside database migrations so we can see that these are the default migrations what we got after LEL installation so let's execute BP addition migrate command to run all these migrations and create their respective database tables so I will go inside project terminal if I clear console and PHP addition migrate so before pressing enter as we go and see that inside this migrations folder we have four migrations press enter so as you can see that here means apart from four migrations also we have additional migration files automatically migrated and created tables inside our database now the question is from where all these migrations comes from so these are basically comes when we had install all about Lael passport package so once we done all about the successful installation of LEL pass passwort authentication package inside our setup so behind the scene automatically it has also created some migrations by the help of that we had migrated and created few tables inside our database like a refresh tokens personal access tokens or clients or o codes and access tokens so successfully now we have completed our step number two to migrate all the migrations and create respective tables inside database now let's go inside step number three so here we need to run a command of BV artion that is PHP artion passwort install once we execute inside proac terminal it will create personal access or tokens so let's copy this command go to Project terminal and also when we had install LEL passport authentication package so automatically after installation it has provided a command called passport install so let's go here if I type PHP artian passport install remember this command is only available if and only if you had installed successful LEL for/ passport package inside your setup so let's press enter now we can see that successfully it has generated some keys like client ID 1 secret key client ID 2 and secret key so finally now we have done all about step number three let's go to step number four so here we need to open auth.php file from config folder and inside that file we need to search guards and inside guards we need to add this API array so first I will go inside our project setup let's close this file and inside this config folder I will search for auto. PHP and here here I will search for guards inside this guards we can see by default we have web now inside this array I will add one more key that is API so let's copy and put it here after this web let's go again to the documentation next we need to search for providers and inside providers we need to confirm that this code is available or not so I will go inside auto. PHP of config folder search for providers and we can see that here we have users driver equal to eloquent and model equals to app models user this is user model class which is associated with users table so it is okay inside providers because as we know that for authentication to store our details of user we are going to use users table and user class so all we need to confirm inside this providers that this value is available inside providers in your case if suppose you are using some different tables for authentication like employees authors so simple you need to Define one more array here inside driver eloquent is okay and inside model you need to pass your own model which is associated with that table so this is all about step number four now next I will go inside step number five and I will open user.php this is user model class by default we get after Lael installation and this model class is associated with users table so inside this stab I need to open user.php from App models folder and here inside this model class I need to update few lines of code so first I want to go inside project editor let's close this file and I will go inside app models let's open user. PHP so here we can see that use level back/ sanctum back/ has API tokens so as we know that by default level 10 provides sanctum authentication means it's by default Behavior but we are using passwort authentication so this time instead of this line I will go and remove and I will add this has API tokens from this passport package after that we need to go inside this user this has API tokens already added here because in last time this has API tokens let's go and undo that this has API tokens is coming from sanctum package but here we want to use the token based concept from password so instead of importing from sanctum what we can do here use lavel back/ passwort back/ has API tokens so once we add this line of code go inside user model class and use this has API tokens so successfully this is all about the settings of user model class let's go to documentation so once we add that line of code use has API tokens so this is all about only the settings of user model class so once we complete the settings and configuration from Step number one to step number five so after successful completion we get a middleware called o API and this middleware we can use inside those routes where we want to protect them for example in this case we want protection of if we open HTTP controllers API API controller class so here inside these apis we want to protect profile and log out so while declaring these routes we protect using of API middleware so successfully this is all about the settings of passport authentication package inside our setup so this is completed now in the next we need to create our API routes so to create API routes I will go inside project editor let's close these files I will open two files here first I will go inside routes folder let's open api.php first and next I will go inside app HTTP control controllers API and API controller class this is our routes configuration file and this is our main API controller class from where we call our API methods so first inside this api.php I will import API controller class and once we add this line of code simple first I will go and comment these lines or simple uncomment it is up to you so I will go commment these lines first I will Define routes for registration API and login API so here we have two set of routes first is all about open routes and second all about protected routes inside open routes open routes are those routes where we don't need any login or any token before accessing them so here we have register method login method so we don't need any login or any token value so these methods will be inside our open routes but if you're talking about protected routes so these routes like profile and logout before calling these methods we want user must be logged in and also we want a valid token value so these are protected API methods so here route I will call Post method so first will be register array API controller class and from this controller class we'll call register method so this is our first API route next I will Define one more method inside open routes that is for login array API controller class and from API controller class will call login method so successfully these are two open routes next we need to Define all about protected routes and these routes are those where we'll use the concept of middleware as we know that we had install LEL password and successfully we had done all the settings with our application so we have a middleware called off API so before calling profile API method and lout API method we need to use the concept of all API so here route I will use group method let's define an array inside first parameter I will pass a key called middleware Arrow operator array and here our middle rare as all API inside this second parameter of group method let's define a callback function and inside this call back function we need to Define our routes so here route I will call get method profile I will go inside API controller class and from that controller class we'll call Profile method next we need to Define all about full logout s out get method log out API controller class and from that controller class we need to call log out method so successfully this is all about API route settings and one more thing whenever we call any API route from here like register login we need to add API as a prefix while calling any API from this file for example if we want to call register API simple syntax will be Pro URL project URL after that we need to use AP prefix and after register it means that before calling any API route from this api.php we need to add this API as a prefix before every route so this is all about the syntax to call any route from this api.php so if I go to our checklist successfully now we have completed this step number five now finally we'll see all about the core part of the this complete video is all about API development processes so first we'll discuss all about our first API of this course is all about register API by the help of registration API we to create some users and save inside our database table so before creating any any user if I go inside our database table let's click on users table go to structure so before creating any user what the data we want so we want name email and password so these are the values we want inside our request we get all the values from request and save inside this users table by the help of user model let's go inside our project editor I will go inside this API controller and for registration API we need to work over this register method so before starting all about the concept of register user as we know that first we need to put all all about data validation so here I will use the concept of this request object to call validate method so here data validation so request I will call validate let's pass an array and here we need to pass key value Pairs and key will be the column names or the body parameters name what we pass while calling this register method so here we want name it will be required next we want email it is also required and we want password it is also required so these are the basic parameters we want to create any user for users table along with all these fills some additional rules for this email that whenever we pass any email value to create any user the email must be a email pattern so here email will with the second rule next we want that whenever we register any user with a specific email so all the email of all the users should be unique so here I will use one more rule unique colog and the table name it means that the email address inside users table will be unique in the password We'll add one more Rule and this will be confirmed so successfully this is all about our first step app where we had implemented data validation so after the concept of data validation next we need to use the concept of user model to save our user inside our table so first thing first we need to import user model here so use user and it is coming from app and models I will go here let's add a single line command that is create user so I will call user model and call create method inside this create method I will pass an array here we to pass key value payers here key will be the column names of users table so name email and password so for name value we want to read the name value from request here so request object and email not email this is name here next for email request email and for password it is request and password and one more thing before saving this password as we know that this is a plain text value so we want to encrypt before creating any user with this plain text value so what we can do here to convert a normal text into encrypted value I will go here and import use eliminate eliminate support facad and we'll call Hash facade once we call this hash faad simple just copy go here and I will call Hash call make method and inside this make method pass this simple text value now this text value will be converted into a has value by the help of this hash facad and make method and finally after creating user we to return a Json response so return response I will call Json method let's pass an array status equals to 1 message let's say user created successfully instead of one let a true here so successfully this is all about the complete logic and the code implementation of register API method let's do a quick unit testing I will go inside Postman let's start development server BV artian serve press enter started I will copy this project Ur go inside Postman put it here let's select post here because we'll hit register API method using post request type so post here go to headers and I will first pass accept application Json once we pass this header let's go inside body select form data going to pass the key called name email and password so after putting all these columns because we have our data validation here so we want to pass all these values let's pass our first user and I will pass a password from 1 to six and before passing any data let's go and first complete our API URL so API is a prefix and register so I will go and click on the submit button without passing any data let's see what happens now we can see that here we have bunch of errors and this is because we have implemented data validation now let's pass this name email and password I will first pass its email value without having any email pattern let's click on send button now you can see that email fi must be a valid email address so I will go and let's pass a valid email pattern I'll will click on send button again we have error that is the password field confirmation does not match and this is because as we have added a rule in front of this password with the name called confirmed so once we have added this confirmed here it means we need to add additional field here inside this request with key called password _ confirmation and inside this key we to pass the exact value what we have passed with this password key so here it is 1 to6 if suppose we have passed 1 to 7 these two Fields didn't match click on S button again we have the same error let's go and remove this seven here this time we are hitting this registration API with all the valid values click on send button now we can see user created successfully let's go inside our database go and click on browse button now we can see that name is okay email value is okay and also password is a hash value this is what we have used the concept of hash make method so successfully now we have created our first API that is register now let's see the next concept from this playlist plan that is all about for login API so to work with the login API let's go inside project editor I will go inside API controller class and this time we will work on this login API method so here first we'll Implement all about data validation inside login API we need email address and password so here request I will call validate method let's pass an array here we won't inside our request the keys will be email and password let's add validation rule it is required and also the email value should have a valid email pattern and this password is required after data validation let's say that we have passed an email value and password now this time we'll use the concept of or facad from or faad will call attempt method and inside attempt method we need to pass email address and password so automatically behind the scene it will detect and match our credentials with the database user so first I will go and let's import all facade so use eliminate support fads and I will call a facad I'll just copy go here inside this login method so here we are checking user login so or I will call attempt method and inside this attempt method we need to pass an array here and inside this array we need to pass the email value and password so email and password inside this email we can get from request here so email and password is also from request so once user will exist with these details this is going to return the true response otherwise false will be returned so in case of True Value let's go and wrap inside this eel block so inside true response we go inside this if block it means user accessed otherwise I will go inside this else block let's return response Json let's pass an array status equals to false message equals to let's say invalid login details next I will go inside the safe blog it means user exist so here I'll read the user value so user equals to or and user method means or is scope resolution operator and user method so by using this user method from this o facad here it will return a user object and inside that user object we have all the user values now next I'll generate a token by using this user object so here this is all about authorized token value what we will use inside for the next apis like profile API and logout API so token equals to User it's a user object what we getting from this o user I will call a method create token and inside this method we to pass our token name so let's call it as my token now this method will return a bunch of tokens into different different formats so from those formats we want to access only access token value so finally by the help of this line we are getting a access token value and this is the concept what we had used because inside our user model we have has API tokens what we had implemented of LEL passwort let's open first all about user model class so here we have added level password also we are using has API tokens that's why we called create token method now next here after getting this token value let's return response Json method status equals to True message equals to user logged in successfully and finally the most important parameter of response that is all about token and here we have the token variable if suppose you have not done these settings in a correct way so in that case you will get some errors while calling this create token method now successfully everything has been now completed for this login API method now let's do a quick unit testing I will go inside Postman let's copy this API URL off register open a new tab pasting it here first I will select this post request type instead of calling register I will call login go to headers let's say accept application Json application Json go inside body select form data we need to pass email address and pass password so these are the two values what we are passing inside request of this login API so here for the time being let's copy this valid email pass it here if I pass let's say a invalid password click on sent button we can see invalid login details but when we pass a valid value of password from 1 to 6 everything is okay click on send button now we can see that status equals to two user logged in successfully and inside this token this is the most important value this is the authorization token value of passport and this token means the complete token value we to use while calling profile API and logout API so successfully this is all about the concept of this login API now finally it is also done now in The Next Step let's see all about this profile API so profile API simple means that when we call our profile API it means when we pass the authorization token value inside header first we need to get the token value and from token value we need to retrive the current login profile data let's start all about the concept of this profile method so before working over this profile method if I go inside api.php so as as we have discussed that these are the two routes called profile and log out our protected API routes it means before calling before accessing these methods we need to actually go or bypass from this off AP middleware in case if we don't pass any token value means valid token value we are not able to call these methods but in case if inside our request we have a valid token value we authorized to call these methods so in that that case inside these methods we have a faat available so by calling or faat and user method automatically we can get the user profile information so here user equals to or faad and I will call Chooser method so here return response Jon method and let's say status status equals to True message equals to profile information and let's pass data equals to user so successfully when we pass a valid token value while calling this profile method automatically when we hit with the authorized request to this profile method here inside that we have a all facad and whenever we call user method automatically it it returns all about user profile information so let's save these changes I will go inside Postman copy this API URL open a new tab pasting it here let's call it as API SL profile and I will select here get request type go to headers accept application Json and also this time inside headers we to to pass one more header with our valid token value so I will copy the whole token this is quite long so I will copy the whole token go here and add authorization token value it's a header and here let's a be error space and the token value let's say I'm not going to pass this authorization inside header now let's call click on send button as we can see unauthenticated because this request the API call is not able to bypass this middleware so it returns from this stage and we are getting unauthenticated let's pass this authorization and I will go and do some mistake here let's remove this e Click on send button again this time this is a invalid token so this is not able to bypass all API middle and we are getting unauthenticated let's go and pass the correct value value this time we have the valid values hitting this profile API method click on sent button now we can see status equals to True message equals to profile information and inside this data object we have all about the user profile information so successfully this is the concept of profile API method and it is also completed now finally we'll discuss all about the last API of this course and that is all about log out API the concept of logout API is simple whenever we hit log out API it means it destroy all the sessions of logged in user and it destroys all the tokens and after destroy after log out whenever we try to use that token value inside any authorized API then automatically it will be unauthenticated so let's go and see all about actions first so inside this log out and one more thing that as you know that this log out method is also protected within this o API middleware it means whenever if we able to hit this log out method so here we have all forart available so either inside LEL we can use or forat or Ora function to read all about current logged in user session so this time inside this log out method we'll use the concept of orala function what I mean in the last API that is au user if I copy put it here so this is the same concept if I call or and I will call user method these two lines will go and return the same value either we can use or facad or or ala function so this time inside this load API method we need to use or ala function user method and I will call token and I will call revoke me method by the help of this line of code simply from o we are reading the current user and from current user we are fetching the token value and simply by calling this revoke method we are destroying that and once we destroy the current logged in session tokens value then finally after log out we are not able to use inside any of the protected apis so here let's return response Json status equals to True message equals to user logged out save these changes go to postman copy this API URL open a new tab select get here let's call it as log out I will go inside headers let's accept application Json and let's copy our token value what we have generated by the help of login API so I will copy go here after this accept let's authorization beor space and the token value if we don't pass this token value while calling this log out as we know that this is protected API so hopefully we'll see all about unauthenticated now let's pass authorization token value click on send button so whenever we press this send button automatically it destroy the current token value of current logged in user so let's go and click on send button user logged out now the same token value we had used inside this profile API and as we know that this token now destroys click on sent button we can see unauthenticated so this is how magical concept of logout API so finally the last API concept also we have completed of this course and one more thing before closing this video this is quite interesting that as we know that we have inside level all facad and all hel function so either we can use either we can use o facad to call attempt method like in this way also you can use all Hela function and attempt mathod this is the alternative way to use or faad and orala function if you use this or attempt or orer function the results will be same so here inside this or attempt also you can use Ora function and attempt method and also here orala function and user method so like every stage what we have done with all facade you can simply replace with allala function so finally guys I hope that you follow this video from ground label like from LEL installation up to API development processes so if you follow this video from Step number one to step Number Nine without an escape so I hope I 100% sure now you will have some confidence to create level 10 apis using passport authentication so finally this is is a stepbystep course where we have seen all about the detailed concept from installation up to passport authentication installation and it settings to create API routes and API settings and API processes with standards inside LEL so successfully now we have completed this complete course of LEL version 10 rest API using passport authentication and I hope this video is too much informative for you so please like comment to this video subscribe to our YouTube channel and let's meet into our next magical concept of video so for this session guys thank you for watching and have a great day
Info
Channel: Online Web Tutor
Views: 6,408
Rating: undefined out of 5
Keywords: Laravel 10 APIs Development, Laravel 10 RESTful APIs Development, Laravel APIs Development with Passport, Laravel REST APIs development using Passport Package, Step-by-Step Laravel 10 APIs Development Tutorial, RESTful APIs Development Using Laravel 10 and Passport Authentication, What is Passport Authentication in Laravel, How To Create APIs in Laravel with Passport Authentication, What is Laravel Passport Authentication, Online Web Tutor
Id: Uz56BOekpLA
Channel Id: undefined
Length: 52min 6sec (3126 seconds)
Published: Wed Nov 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.