Laravel 8 API authentication with JWT on PHP 8 #laravel #jwt #php8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to portfolio and in this video i am going to discuss laravel 8 api authentication with jwt token on php 8 platform so we will use this package diamond readability auth but this package has a release the compatible package with php8 so we will install it with the help of some tweak okay and also this documentation is a laravel for a laravel 5.4 or below so we will make some tweaks to implement this on our laravel 8 okay so currently i have installed the laravel 8.69.0 okay on php8 so as you can see okay php 8.12 okay and to install this time diamond package we will ignore the dependency check okay so there is a flag you ignore platform x okay and just install it it will take a while to install okay installation is complete and so after the installation let's follow this step and we will make some tricks according to our requirement so let's first of all copy this to make your laravel know that we are going to use this uh jita putty earth provider okay so let's open the app.php file and just close the command prompt for now and in the provider section let's uh paste it and then the next step is to generate the configuration file for jwt so the command prompt let's paste it and enter so it will generate the jwt dot config file in your config folder here and it contains the server configuration appear related to jwt okay like a keys and the expiry time of token okay so you can go through then the next step is to generate the secret key of jwt so let's generate it and it is successfully generated and you can verify on.any file bottom okay so the next step let's click on next on this page it says to human human okay so the next step will be to modify the user model to implement the jwt subject contracts okay so let's open the file okay and just paste it okay and to implement it and start wt subject it has uh two functions so we need to implement those also and let's copy those from here and just paste it okay and now configure auth card so let's copy this part okay and open the earth.php file in your config folder and paste the copied text here so we are declaring here the card for api requester okay then the next step is to declare the roots copy and open the roots api.php file let's comment it out don't record this and paste this so you can see it is using auth controller so we need to create the auth controller also so let's create it first okay and it's generated and let's open earth controller and just copy and paste this code there we will make some changes to [Music] make it work so let's uh go through one by one we have a login function it is using all the time currently so we need to change to use our jwt auth attempt like this okay to also declare uwt earth basically it is it is the facade basically so let's include it this one okay it is facade and now uh the error is gone then then login function it first check the credential and then generate the token and if it is then if the credential is okay then you generate the token with the respond with token function response with token function also return the expiry time of token uh we will [Music] return our jwt dot php file ttl config here okay so let's change it also dot j wt dot detail okay like this also there is a registration function missing so let's create it quickly to register the new users register okay and let's validate the incoming request for sword okay and let's catch all the parameters and in the second parameter we will take three parameters first one is the string name and that will be required okay second is the email as we have three free level fields name email password you can tweak also i'm just creating here the basic registration function you can customize as per your requirement it must be email and that is required okay and third one is the password i'm not taking here the confirm password but you can take if you required and main length let's say six okay so if the validation failed suppose so we will return the error message to the user using the api okay response json okay if the validator fails and return the response okay as such restriction field let's say okay and just a message and if the validations sucks succeed then let's create the user because i create name will be request get name okay second one email request get email and third one is the password to encrypt the password let's let's use the big crypt function yeah request get password okay and return the user the created user in response okay we have error here and the justin message will be let's say users created and let's return the user the created we have created okay so that is the basic registration function also we need to declare the user model on top so like this okay and now registration function is created login is okay me just return the current user logout just successfully log out the current user refresh token return change the current token generate new one okay and it's a wrapper function that will return the user with expiry time and it tells that token which is a bearer so you need to send the token as appear okay and in the api.bpa.php here we need to tell the that varies our earth controller so let's declare the namespace here app http and let's copy here from here and just paste it okay and also declare the register function post the let's distro it okay ah you're good to go so let's uh test it the help of insomnia rest client so i am sending the theory parameter name email and password okay this is our path so before that you need to restart you need to start basically our variable instance local server php autism serve okay so so our local server has been started on port 8000 and uh let's test it probably not define okay in the earth controller we are using the middleware auth api okay except the function except for the login function also we need to declare here our newly created register function and now let's check it the spelling is incorrect okay also uh let's create it first so uh we have created our user successfully okay it returned the message user created and the user and let's login with this user so for the login we are sending the email and password okay and also you can check your database if you just created not the user in the user table we have created mochi one two three okay and let's login with the login path and i'm sending email and password and the header is your firmware encoded and send so it returns the access token and token type b error and expiry is a 60 second okay so let's copy it this token and on the user profile that's the me path okay it's a get and we have declared here in our roots let me uh let's change it it's supposed okay it runs uh it will return the current user okay and let's send the bearer token insomnia it's a bearer tab under the edit as well you can paste it your paste your token if we access this path without the token so it will throw the error it also modify this response we will change it later so let's for now paste the token enable we'll get the result okay so now our authentication jwt application is working also the refresh token and we can check here so it it will return the new token okay so if we uh using the same old token that that will not work like this okay also you can customize this response simply there is a middleware auth middleware authenticate middleware okay and it simply it extends the middleware class and the middleware class has the authenticate function as indicated function so let's copy it and paste here and instead of throwing the exception uh we will use the board function you can use the word function here okay and the board function let's return the response and the authentication code the error code this spawns okay and you can customize your test for your requirement okay and now if we check this then it we will get this okay so in this way we can we can implement this package okay this time and jw2 package okay hopefully this video helped you will help you and if you like it then please subscribe and like my channel okay thank you thank you guys
Info
Channel: kode patcher
Views: 143
Rating: undefined out of 5
Keywords: latest tutorial, laravel tutorial, laravel advance, laravel latest, laravel, laravel interview, laravel relationship, laravel framework, laravel php, laravel api, token auth, laravel token, user token, laravel 8, laravel package, larvavel authentication, larvael package, laravel jwt, jwt, php8
Id: 1FRPX6W3GJY
Channel Id: undefined
Length: 21min 37sec (1297 seconds)
Published: Sun Nov 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.