Laravel 7 Passport – Create REST API With Authentication 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi good morning um today we'll be having a tutorial on laravel passport authentication we also build a simple product crude using lava passport authentication lava already provides traditional login form authentication but if you want to use api api uses token for authentication for authenticating users because they don't use section when a user login via api a token is generated and sent to the user which is used for authentication purposes lava provides passport to work with api authentication without any difficulty so let's see how to set up and configure larval passport for api authentication and rescue apis in a larva application so let's start all right so to start the first thing we are going to do is to install um a level projects so i'm just going to copy this command you should be used to this right now so um though my network is a little bit slow so i'm just going to pause the video and once it's done we continue yeah all right guys um our installation is done so the next thing we are going to do is we are going to open this uh folder in a new window so we're just going to open in file explorer okay so this is the folder so just open the folder and we open it in the escrow again and close this one but all right so now we are back so these are working folder so the next thing we're going to do now is uh we're going to install passports we're just going to do composers so we're just going to do composer all right so just do composer require laravel slash passports so let's just um allow it to install so why it does that so just pause the video in a moment all right so we are done installing our our passports so the next thing i want to do is we'll open our xampp and we'll create a database for our larval projects so these are example loading all right so let's create a database for it so let's say lara pass spots so so now we back to our project and we'll go to our environments my system is just being slow and necessary all right so in database we just put lara passport all right guys so this is all we need to do for now so the next thing we are going to do is um we're going to go to our config [Music] go to app and uh what i want to do here is um we want to add passports to our service provider so we just come under the section where we have provider here so this is what i normally do i just kind of like you know go down a little to know the one i had it so say lava [Music] hospitals passports service provider and class so this is it so laravel passports they will call the passport service provider so that's after that then the next thing we're going to do now is we just come here and just say php artisan migrates all right so our migration is done so if we check our level the database you're just going to say something now see all these auto access token this way have been added by our passport so all right guys so we are done and we are good to go so the first thing first uh we want to do next is we are going to do php partisan passports install so the reason why we did this is to generate an encryption key that is required to generate a secret access token okay this is just kind of like um you know the standard all right so next thing is i will go on that app on the user so here we need to change our allower application to be compatible with the passport configuration so the first thing we want to do is so we're going to have um use lava passports has api token tokens all right so once we are done with that we just come here and we just do has api token okay all right guys so this is all we need to do for now so let's move forward to creating our authentication service provider rates so to add uh passport routes what we need to do is we need to go to the app folder then we'll go to the provider then um from the provider we'll go to alt service provider then inside the alt service provider first thing first is we'll first of all enable the model policy then after the model policy the next thing we want to do is we'll add use level level passports passport all right so this is um so we add the lava passport to our old service provider so the next thing we want to do is um we'll go to the boots function and inside this boot function we just had a passports roots so guys what this actually does is you know once this just automatically creates all the necessary routes that is required for passports all right so this is what this does so instead of you creating it uh you know manually this just does everything for you so all right guys so the next uh stage now is we need to con edit our authentication so we need to edit our authentication and that is inside the authentication folder so we just go straight to the app all right so inside is um up we're just going to add the passport to our api um feed just a second sorry is the odds folder not up so inside is alt folder radar is going to come down to this section that has api and we're just going to use provider drivers the driver is going to be passport and we are just going to comment this harsh hair all right so this is what we need to do so all right guys so let's uh start creating the roots for the projects all right so to create the roots what we're just going to do we just go straight to the root folder we'll go straight to api all right go straight to api and so this is uh what we're just going to do here so the first thing first is we just call right now so we're going to press the post through tab for login okay i'm going to create a poster for login and start passwords okay so i'm just going to duplicate these and okay so in case you're wondering how i did that just press ctrl d it does if you're using um vs code so just highlight and press ctrl d see it that's how i do that all right so inside our middleware so i'm just going to like uh you know refactor this a little so the first thing i'm just going to do is uh i remove the okay let me just write it from scratch so it has roots so i'm going to call the middleware and in the middle where i'm just going to do us api so so what i want to do to group this middleware right so now we're just going to call our difference so the first route i'm going to create is uh our user roots so it's a gets so i'm creating it gets roots so user passport controller so this root is for user details sorry guys uh supposed to do that on my quotes all right so the next route we're going to create sorry so the next fruits we're going to create is for resources so these roots has to do with everything that has to do with our everything that has to do with our products so we don't need to be creating um separate fruits so everything that has to do with products we use this particular all right so that's products and so we're just going to use um call up products controller here so we haven't created all these so we'll create it as we progress all right so we have successfully created the roots the different routes so here we created a the login and authentication route so why here we created the user roots and the products roots and we put everything inside the middleware which is our authentication api middleware so all right guys so we have progressed later so let us go so the next we want to do now is we want to start creating our our controllers so the first controller we're going to create is our passport controller so that's phpr system make controller and we're just going to call this hotspot controller all right so we've created that my system is kind of slow today i don't really know what's going on all right so so all right guys we actually had an error here okay so our error came because we didn't close our roots our middle way so that we had an error so this is the area so i'm just going to like run it again and now it should work properly all right guys so that's it so our controller our passport controller is created so you can just use um control p and just say passports uh so so this is our passport controller so here we're just going to create uh everything regarding our passports logic here so all our passport authentication logic will be here all right so let's go so the first thing i want to do is i just want to import the user the user model okay so the first thing we're going to create is we're just going to create our registration controller it has a public so public function requests all right so what i'm gonna do is the first thing i'm going to do is i'm just going to create a a validation method here to validate all our inputs because some users can be very very malicious so you don't just want to trust anything that the user is giving to you so that's why you must do this is a must right so since uh user will be passing in name giving us their names so we have to check it and we're saying it must have a and we say it must have a it must be required and it must have a minimum of three so somebody cannot just come and put type nonsense there that is must have a minimum of three characters so the next one i'll validate is the email so i want to make sure that the user is actually giving us an email and we want that email to be unique right okay so this is a database that we are checking it across so we check we're making sure that in the user database is actually unique okay that particular image you need so all right guys so the next thing we're going to check is uh the password so so require minimum the minimum is six and um so we are done with that so we just close it so the next thing we want to do is we want to create we want to create a user so we create a user variable and these user variable we call the user model and we do create so this is a lava eloquent method creates so oh no all right so what do you want to create the first i want to create is name and i want to pass it the requests the request name which is the name we are getting from our phone rather from the api rather okay so the next thing we want to do is email all right guys just a quick um just a quick one please when you're creating uh when you're creating an object or when you're trying to store data into your database make sure that this particular this particular name corresponds with the one in your database all right because if it does not correspond you're just going to have an error all right so the next code we want to create is our password and um so far password we want to like encrypt our password so what we the so we'll be using decrypts so big crypt is actually really good okay it naturally sorts your password which makes it difficult for our cars to you know to guess all right so password and yep oh all right so we've done that so we're going to close this so the next thing we want to do is we want to create a token so if the user successfully uh if a user successfully register i want to create a token for that user and we're just going to use the create token create token and okay so we just give it a variable um and lighter you can actually put anything in here yeah you think or you want here so it does become electro academy all right that sounds cool so let's say access token okay so so once you return so i will return once you return a response i want to return a response and the response we are going to return is adjacent since we're working with api json is the best way to return your data and the next thing we want to do is uh one who creates an object adjacent object and it is going to pass to this so we created a key called token and that key we're just going to pass in this token variable okay all right guys so after that so we're just going to pass in a server response of 200. okay so let me just do a brief a brief um a brief walkthrough so what we did here we created a register function and inside this function the first thing we did is we validated all our inputs or rather all the that's coming from the user so the next thing is we created the user objects we pass the user data to our database by creating it here then after that we created a token for the user and we returned the token back with a server with a server status of 200 all right guys so this is everything we need to do consign our register so the next thing you want to do now is you just want to create the login function all right so the first thing i want to do is credentials all right so we're just going to save everything that a all the user credential in this variable called credentials and um we're actually going to pass it into our parry okay and uh next to what we do is we create a key and positively all right so just to make it first okay so all the users credentials we're passing it into this array okay and um so the next thing i want to do is want to create an if statements to check if the user's credential is actually correct so next thing first we just said odds we'll call our odds method and we just say attempt okay and we pass it the credentials so next i want to do is um we'll create a token if if the user credential is correct i want to create a token for the user all right so we just do what user i'm creating i'll create a token create a token and i'm just going to copy this and light your account in here and just paste it here okay so let's gonna do access token and so we created a token for the user so we want to return this token we created so it does say response basically what we did the first step in the users in the registration section so it is going to say json and so create a key token i will pass it the token we just created so we just created and we return the server response of 200 all right so this is when the user's credentials are correct so i want to say when the user's credential is not correct so if the user's credential is not correct just return the response and this response is just going to say that this user is not authorized okay this user is not an authorized user so it does say on all right guys we almost there and we're just passing this server response so 41.401 and we close that so so that's all about our login function so we've got to use our credentials from the requests then we did a if fair statement we attempted the user credential to check if it's correct and when it's correct request the token for the user i will return that token.json all right guys so the next step now we're going to do is we want to create a function for for the user details okay so this function is just basically going to return user dt so we're just going to do return return respawns jason okay user odds i'm actually not a good i don't really type first so please bear with me slow typing it's just my thing but i'm improving all right back to so what we just do here is uh so we can get the user data and everything related to this particular user so you can modify it the way you want but just for this tutorial we're just getting everything from the database that has to do with this particular user all right guys so far so good we've created our login and register functions and the details so the next thing we want to do is we just want to go next to the next section of this tutorial which is to create uh our product code all right so that is what we want to do now all right so create our product crew crowd some people call it crowd but i call it crude whichever one is correct people all right so we'll do php artisan make model so that's a product product so we just wanted to make both control make a model and a controller for us all together all right guys so uh products controller mode has been created so the first thing uh we want to do is uh we'll go to our dates base migrations and yeah this is a product table we just created so we're just going to add some more teams here okay so so for our table we're just going to create a a column for user id and it's going to be an integer okay so next i want to do is we want to create um a column for names product name and since it has to do with a name so it's going to be a string so it is going to say name and uh the next order they want to create for price so i want to create for price and um sorry price is actually an integer see all right guys so um i want to create a reference as well so we're just going to say table we're going to create a foreign key for this all right guys so the next thing i want to do now is um i want to create the fillable property in our products model so we just go straight to app um products so inside the fillables we're just gonna create a protected class for that so put protected our fillable name so the truth is one of these just name and price all right so once we're done with this so what i want to do now i just want to do php artisan migrates okay okay we have an error so let me find out what happened here oh sorry so what actually happened is we didn't we didn't close we didn't close these so now that is closed so that's why we add an error ah oh sorry so you guys gonna do php artisan migrates okay so we still having an error just a moment let me figure this out all right guys this foreign key stuff is just giving our issues so what i wanted to do i just wanted to make sure that every you know the product that is being created is tied to a particular user but just to save our time i'm just going to pull it off and you know try and migrate it like that oh man okay so let's let's see so let's check our table to see what's happening all right um okay okay okay okay okay so our table is already created so our table is already created and so our table is already created so all right so let's move forward so the next thing i want to do is um so the next you want to do is under day under the user the user model under the user model we're just going to create a a public function here public public card products and you just do return has many okay so we are moving straight now to our product controller which we created so inside our controller so it is a product controller which we created so the first thing we're going to do is we're just going to create public so we're going to create an index an index method here okay and what we just want to do is we just want to return all the products for that [Music] so and um so in our so we're also going to import before at least i forget so we're going to import our products model how am i doing okay okay so i've created so the next thing i want to do is uh inside our we want to return so we're not going to return the response and this response um we're going to return json as usual and uh jason is just going to carry a success his subsex message telling us is successful i'm just going to return a boolean sent true and we're just going to return data so the data we're going to return it's uh the products so it is going to be tiny products products products products all right so let me close this before we run into an error again so now that's for the index so let's say you want to check um let's assume you want to check for a particular product a single product so we're not going to create a method called show an initial is going to take a parameter called id and uh we're just going to create a variable as for d initially so this variable is going to call and you know as usual we check if the user is authenticated then call the user model and recording products and do find so the particular user i want to find just say id all right so that is the first stage so next i want to want to create an if statement say if actually there's if there is no product if there is no product i should return a response saying that there is no product so return so you're going to return your response and the response is going to be adjacent i'll show you so i'm just going to copy everything like here just to make it fast and so it's going to return for us because here we're just going to return a message and say that we're just going to recreate message let's say that the products with id so we're going to concatenate this id receive product id concatenate is not found right so this particular product is not found and uh so as usual we're just going to return 400 error 400 which means not found all right so we're just going to write so next we want to do is does if that particular product is not fun but if it's found want to return a response all right that says return adjacent okay so it doesn't mean as usual you're just gonna you know copy everything here okay so we're gonna return through so we're just gonna return your products but we're going to use tony on harry it is going to go to harry and as usual we close and over here we just put some 400 okay so now we are done with this we're done with this the next thing we want to do is so let's uh let's want to store a new product into our database so we're just going to create a public function so we're going to create a public function again for that now we're actually going to store so as usual request request so we're going to validate as usual so want to validate we want to validate all our user inputs because no user can be trusted except the developer of this system but believe me no single user on this heads can be trusted so that i want to validate so this is server side validation so you can also do html validation as well right just to make sure that you know we are not missing up anything so i want to validate the price so i want to make sure that for the price is actually an integer so it is going to be required and integer alright so we are done with our validation we just close it here so now we have validated so what i want to do is we want to create a a product model so we just say products and we create a new model for products and we close that so next i want to do now is we want to say products which is free and we're calling on the name and we're going to pass it the request the user input okay okay so surprise as well all right guys so now so what we'll do this is we're just going to create an if statement say if odds if this user has the right credentials okay save this particular product right so i want you to see what this particular product oh sorry mistake all right so so when it's been saved you want to return a response and this response is here json i should have just pasted this right oh my god all right so i'm just going to paste it there success products so to hire so harry metal and just close it all right so so else else if the user is not authenticated else so please kindly return so it's going to return a response now what let me just copy this stuff here see let me change this all right so first of all let's turn this to false and you're saying products could not be found all right so product number found so so let's close it and okay okay so now and uh guys it does miss some series of mistakes hey so the first thing i want to do is i want to wrap everything this is my jason inside in a coil in a brace and give it an error of 200 and here we're just going to wrap it inside the brace sorry guys for this mistake sorry i'm really sorry i'll just say 500 okay okay okay see series of mistakes i'm sorry guys i just i don't know just distracted all right okay guys um so yeah i want to return 200 as well okay okay so so far so good this is where we are currently and uh so let us let's progress we are almost done you're almost done you're almost done you're ready there okay so i want to do now is want to create a function and this function we're going to create for updates so if you want to update our products and requests requests and i want to get an id so this is the id of the product the product id that i want to update so um guys i'm just trying to make this video as fast as possible so i'm not going to copy this file id here so i'm just going to paste it here all right so let's do that so so the next thing i want to do is um i want to create an if statement so what what i want to do first of all i just want to say if there is no product all right okay if there is no product rather this product could not be found okay so i want you to return a response and this response is going to be adjacent and all right so we're just going to return success okay all right so let me just copy this stuff okay so we just said um this the products product id products with id then we put concussive names and say id concatenate again and we say not found all right so that's that then let's still want to do now is as if it's nothing but if the product is found what i want to do is want to create an update updated i will just say products okay i want to fill in all the requests all right all the updates so does it feel requests all and so we save it all right and uh the next thing i want to do is i want to say if you know if it's updated if it's updated okay i wanted to return some api response and what i want to return here is so 200 so just return true just return true and say products updated sources fully all right and so what i do errors okay there's the first so as a product could not could not be updated all right so products could not be updated so the next thing we want to do now is uh i want to create our deletes so we almost at the end of this whole this whole process so want to create our delete function so the guys so public function so just hit destroy okay destroy everything that has to do with this product and uh you're just gonna do as usual as usual i'm just going to come in and copy these hours so we'll find the particular products that i want to destroy i'm going to just try that product stamp we're just going to do as usual just going to say if this product is not found if this particular product is not found who wants to return a message a goodwill message and this goodwill message is going to say hey guys sorry okay could have found this project okay so you're just gonna say we could what find this project sorry this is gonna be false all right so we're just gonna say hello sir hello sir this particular pro that i wanna destroy could not be found we are sorry if we disappointed you with hiding product with id is not found product with id is not found we're sorry okay so we just give it uh missaba response of 400 not found all right couldn't find it to destroy all right but if it's found if it's found so if products if products delete rip is fun please kindly delete it kindly please delete it and i want you to return a response say yay we did it i wanted to say so let's give it a message and say how so it is not it does not supposed to be data so to give it a message of products deleted so six fully all right good so we're on track we're on points right all right so else else let's assume something went wrong couldn't delete a particular product so just want to say return all right say first of all we're gonna return false now we're just gonna say we're gonna return a message we're going to return a message okay say products could not be deleted we don't know the reason so we're just going to return 500 all right okay so guys thank you for staying this far all right so this is the whole stuff so these our delete method our updates our store to store this is show and this is our index all right guys um so the next thing i want to do is i want to test i want to test what we have done through postman but before then let's just first of all run our program php artisan save all right so our application has started successfully so next thing i want to open my postman and test it out all right guys so so before we before we uh proceed uh i'd like to just correct some few things in a passport controller first of all this is wrong so you're supposed to put a comma and respect our big crypt wrongly so just supposed to be tripped all right guys so let's go straight to our uh postman so these are these are register routes so i selected post here so this is the route we are using to test it out so i'm just going to create a new email so i'm just going to call it testing and i leave a victor joe all right so i leave all the information here and so let's test it out okay so this email has been created successfully so let's try and log in this email so i'll just call it testing since the password and the password is the same so i just click next so what happened here is uh now you see a new a token has been generated for us so a token has been generated for so now once now that they uh the login api and the register api is working so let us test out the the user details api so what we're just going to do is we just call me so we select gets which is the default so just say localhost http localhost and 8000 and slash api slash user all right so what we're just going to do next is we just come straight to header and in header the first key we want to put is going to be accept all right and the value for ourselves is going to be application slash json and the next thing we want to do want to put the other key which is authorization and on our authorization here we just put viera biera in front and we just come over here and we are just going to copy this whole token that we generated all right so this token is the one that's going to grant us assets so we'll come back here after the beer i will just paste it here and that is all so if we click send if we click send you see we got our user data back from the database so these are user data which shows that everything is working perfectly well everything is working perfectly well so the next thing i want to test out is we want to test that's the the product api so what we're going to do is we're just going to do um so over here we're just gonna do products products and uh as usual in the header section we just do uh assets accept and uh here we just do application as well as json here we just do authorization okay and over here you just do bearer and we paste our api key token all right and we just do send so you see uh okay there's an error and let me just try and decode that error all right all right guys so i've detected the error so let's try it again so you see uh it showed us an error here so what happened is uh at our product model we didn't close the protected class so we have closed it now so let's go back uh so let's go back and test it out so if it does click send you see it's going to show us true but i didn't return any products that is because we have not created any product at the moment all right so so what i want to do now is we want to try and create a product so we just create our idiom um all right so http um okay so over here we just say products and here we change it to posts over here we give you the first key which is name and uh here we just give it a key which is price and um here we give it a name let's say books i'll give you a prize of 12 000 all right guys so let us um oh test oh sorry many mistakes so supposed to put it inside the body not the parameter not the parameters yeah sorry about that so inside a body say form data name books and uh over here we just give it a price price and we say 10 thousand ten thousand dollars so so let's test it out then okay um we didn't have uh in our header hey we need her our authentication so that's why we have this uh error so let's try and fix that and get it so over here the header we just you know do our accepts as usual and uh we just say application json we just put our authorization authorization and in the authorization we just do bearer before we paste in our api key all right so let's uh test it out okay um we have another area so that's in our product controller so guys so far so good all um errors just basically um typographical issue that's because i need good type i don't type fast so but i'm working on this so i promise that in our next video i'm trying i'm going to improve my typing uh you know spelling accuracy and uh okay we have another area so our next arrow is here so these products so yeah supposed to be products and um so let's let's test that let's test that now all right okay guys so everything is working perfectly well okay so everything is working perfectly well so we have created our products and um so let's go back to uh to fetch the product so let's send okay so you see we have our products here so let's try and create another one let's say um bike and let's say the price is 400 for example all right so we click send okay so if we go back to our api and so we find out that we have um two [Music] so you see we have actually three products in this place all right guys so so far so good uh this is all about um that so let's assume we want to fetch uh a particular product so i'm just going to go ahead where we have been getting our products another state slash let's say let's get product number two id2 so our game again again under error again again what's where did that error come from um that's line 31 okay all right so line 31 so it's supposed to be products not product oh i know what caused this issue because i copied code you see that's why it's not too good to copy it's not too good to copy codes all right i guess that should be all all right so let's go back to our postman so let's check it out yay we got it right now so we have a single product fetch for us so guys um so next we want to do now is let's assume we want to update we want to update a particular product so what i'm going to do i'm just going to copy this url i'm going to come here and paste it here as usual then now here i'll put put since we're updating i'll stick come back here and go to the header so i'll just copy the the host of here come back here ah go to our header so in our header i'm just going to say accept as usual then i'll just do application that's uh json and over here i'll just do authorization all right so i'll just paste the bearer here okay so over here in our body so i just come and do form data so the name i'm going to change the name from books to let's say um what do i call it now let's say mac book oh macbook pro all right guys so next i want to do i also had uh the price so let's say macbook pro is around 1 700 for the recent one all right so i'll just click on testing it out great so we have updated our product world successfully so let's come back here and check if our product was actually updated okay let's check what happened what happened what happened oh so that didn't work so let's just give me a second let me find out what happens okay all right guys see when we try to when we try to update our update our feed is not updating the reason is because when you're using puts you're supposed to use uh x www from your length code so in this place what we're just gonna do is uh okay it just puts our prices all right price and um you just put out let's say 1800 okay and let's check that out so it said updated so let's check out let's check here and you see it's working it's working so let's change product to so let's say product number two i want to give it a essay dell dell server machine machine all right all right so let's play out the paint out let's add one more zero to that all right so let's test it out see it's working success is fully okay guys so the last part of this tutorial is um i want to show you how to delete how to delete so what we're just going to do first of all select the deletes um do http um okay let's delete so so products all right so slash which product you want to delete they say we'll delete product3 oppo dot one all right okay so the next you want to do as usual we just do our accepts and uh over here we just put our application and jason so let us come back here go to our header oh no oh no all right okay so let us copy everything here and over here so we just do authorization and over here we just pasted it okay guys so let's test it out so it said delete successfully so let's check that is it true yeah actually delete successfully yeah guys see it deleted successfully so we just have to product here all right guys if you watch to the end of this um tutorial i just want to say thank you very much for know stopping by thank you for watching to the end all right i can't understand that during the video process like you know i make some typographical issues and that's actually made the video to stay longer and we encounter some few books but i'll say me trying to debug okay for you is uh he's actually a skills on his own debugging is a skill zone so there's no way you can write code actually without you know fixing books so um next time i'll try and improve on my typing scale and i'll try and make the video a little bit shorter so if you have any question you can just drop it at the comment section i will always reply you so thank you and if you have any videos you like us to uh make our we can just simply suggest it i'll make those videos for you so thank you very much please if you have not subscribed to my channel i really happy to you just kind of subscribe to my channel so that i can get the latest of our videos and uh i'm trying to make a commitment right now to be posted at least up one or two videos every week all right thank you guys i know see you now next video bye
Info
Channel: Enlighture Academy
Views: 7,680
Rating: undefined out of 5
Keywords: laravel, laravel passport, laravel passport tutorial, JavaScript, python, laravel passport logout, laravel api, laravel passport scopes, laravel passport jwt, laravel passport vs jwt, coding, code, CSS, HTML, how to, Web Development, Team Treehouse, laravel api authentication without passport, learn, free, laravel api authentication token tutorial, online, tech, java, laravel passport check if token is valid, Web Design, iOS, jQuery, tutorial, laravel 5.8, laravel tutorial, laravel project
Id: eGQ8j6Yr6HE
Channel Id: undefined
Length: 80min 50sec (4850 seconds)
Published: Sat Jul 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.