Laravel 8 Tutorial - API Authentication With Sanctum

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the laravel 8 tutorial in this video we are going to learn about api authentication with sanctum laravel sanctum provides authentication system for token based apis sanctum allows each user of your application to generate multiple api tokens for their account in previous video we have created this card api all right now let's see how can we use authentication in this api using sanctum so first of all let's open the laravel documentation so just click on this documentation link and inside this laravel documentation just a search here sanctum and now just click on this link and for installing the sanctum just click on this link and you can see here the command installation command so just copy this command and now switch to the command prompt and just paste here now press enter all right now sanctum has been installed next step is uh configure the sanctum so for that just run this command so just copy this command and just paste here and now press enter all right publishing complete now let's migrate the migration so forth just right here the command php artisan migrate all right now press enter okay migration complete now switch to the project and now just go inside the app directory and then http and from here just open content.php file and inside this file just go inside the middleware group array and inside this api just copy this this text from here just copy and paste here all right now just open the user model so just click on models and just open the user model and here inside this model first of all just copy this line as api tokens and just paste here and inside this class this user class and just add here the comma and paste here as api tokens all right now save this file now let's create a controller so inside the command prompt for creating the controller just type here the command php annotation make call and controller is controller name let's say controller name is auth controller all right now press enter okay now run the application so just write here php addition server now switch to the project and just open the auth controller so let's go inside the app directory then http controllers and from here just open both controller and inside this controller hot controller let's create some function here so first create function for the register user just right here for creating the function public function and function name register all right and inside this function just pass an argument request dollar request and first of all uh validate the input so let's try it here validate validate validate here make dollar request arrow all and second parameter just write here an array and inside this array instead here name name required so just write your required and second input versus email email request which is also required and also email so let's write here email and for the password password required all right now if the validation fail so just write here the condition dollar just return our response return response as a json and inside this json just pass an array and here just add the status code status underscore code which is 400 and in second message you just add here the message bad request all right and validation success then just get the user so just write here user is equal to user model column colon sorry new user blue user and then user name is equal to dollar request here name dollar user set the email email is equal to dollar request arrow email and for the password user error password is equal to be crypt and inside this let's pass dollar request error password all right and now just call the save function user arrow see and after this just response return a response and then json and inside csn just pass an array and status code latest underscore code 200 for the subspace and message user created successfully all right okay now let's create another function for the login so just write here public function functioning login and inside this just pass your request dollar request and inside this just copy this validation and list here for login name is not required so just remove this all right and copy this line and just here validation is fail then just return status code four zero zero and message battery quest all right and validation done just right here if not for colon and inside this first of all create a variable dollar credential is equal to request and inside this just add your email and password all right and now just copy this variable credentials and past paths inside the certain method all right and if attempt fail then just return response here and inside this just pause here the status code the status underscore code 500 and simply enter the message unauthorized all right and after this if just page here the user just write your user is equal to user column column where and add here the column name email comma dollar request error email just page the first record so just right here first all right and after this login uh just to generate the token so for that just write here dollar token result is equal to dollar user arrow create button and inside this just add here the name fourth token just call here the plain text token then text token all right and after this just return a response after generating this token return response as a gsm and inside just pass here status code 200 let's take this underscore code 200 and also just return this token so just write here token just copy this token result and just paste here all right okay this is the registration function and this is the login function let's create the function for the logout so just write here public function and function name logout and also right here request dollar request and inside just return here right here dollar request arrow user arrow current delete the current access token and after this just return just copy this response and paste here the status quo 200 and here just add the message okay all right now save this file hour and now let's create the route for this function so just go inside the apk.php file and uh here just create the route for the register so just write here route column column gate and inside just add here the uri register then add here the array and inside this array controller name which is auth controller column column class and also add here the function name which is so registered okay so just copy this function and paste here now just create the route for the login route um for the register this method is first don't get post and for the login post add here the uri login controller name auth controller colon column class and function name which is login right now let's create the group route group so for that just write here route group and inside this group add here the middleware so just right here middle we are a little weird just right here the arrow and inside the array just added the middleware name which is earth colon sanctum all right and after this and just create another comma and enter the callback function function and inside this function now just copy these routes in this api cloud route so just select it out and just cut here from here and paste inside this group all right and now inside this just create the route for the logo so just write here route post and at the uri logout controller name auth controller column column class and just add here the function name logo logout all right now copy and paste here now save this file and now it's done so let's check this so switch to the postman and first of all let's register the new user so just open new tab and the method is post and inside the url request url just add here the localhost api and register all right and it's click on body and inside this body just click on raw and here jsn and here just right here name let's say name is smith email and password now let's say password is password right now just click on send and here you can see the response the status quo 200 and message user created successfully now let's check inside the database so just open the database phpmyadmin and database is laravel apidb and just open the user database and here you can see the name of smith you may write this all right user created now let's uh leave the login so inside the postman just open the new tab and method is post url localhost a page slash login all right and inside the body raw in just here at the email id and password so just write here email email is smith gmail.com password and just right here the password password all right now just click on i just uh first of all i'm just going to add here invited password so just write here password one okay now just click on send you can see here the status code five zero zero and match is unauthorized okay now uh let's enter the correct password so just remove this one and now just click on send and here here you can see the status code 200 and this is the token all right now just copy this token and now let's access the this route post route okay so let's open the new app and method gate and uri localhost api and post so let's write here slash boots all right first of all i'm just going to click on send without the token okay so just click on send and here you can see the header okay now just pass the token so inside the header click on header and here just write authorization and value the error space and just get the token all right now click on send and now you can see the post all right now let's check the uh log out so just open the new tab and make a post and uri localhost api slash logout all right and also add here the token so just write your authorization and inside the value in the error stroke and now just click on send and here you can see the status quo 200 and message token deleted successfully now if i use uh this old token for touching the post inside this uh you can see here the old token now just click on send and you can see here the response error all right so in this way you can use sanctum for the api authentication so that's all about api authentication with sanctum so thanks for watching this video and if you still have any kind of question you can ask me in the comment box don't forget to subscribe my channel thanks for watching
Info
Channel: Surfside Media
Views: 14,996
Rating: undefined out of 5
Keywords: How, to, Program, laravel (Software), laravel, laravel v8, laravel 8, laravel project, laravel setting up, laravel v8 setting up, laravel v8.0, v8.0, laravel 8.0, laravel 8 playlist, laravel 8 tutorial, laravel 8 lesson, laravel tutorial, laravel 8.0 tutorial for beginners, laravel for beginners, laravel tutorial for beginners, laravel8, api authentication, api authentication with sanctum, sanctum api authentication in laravel 8, api authentication in laravel 8, sanctum
Id: GwqYx2tIPUI
Channel Id: undefined
Length: 23min 42sec (1422 seconds)
Published: Thu Oct 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.