Laravel 7- Create REST API (Login/Register/Logout) using Laravel Passport

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to my channel and today we are going to learn how to create a rest api using the larabel laravel and the laravel passport and i hope you enjoy this tutorial now in order to ask to get the laravel we need to go to the laravel official page so let's go and search for the laravel and we browse to the documentation and you need to find the instantly installing laravel part so by default we will use the htdocs folder to install our a lot of elements okay so we are here in the actual command line and [Music] so we just have to wait and download and let the composer handle the download it might take a while depending on your internet connection and your machine oh after installing the larabelle or after you successfully installed the laravel uh we need to make great uh we need to set our database and username and password so here i will initialize peace after the migration we need to install the larabel passport package by using via composer so let's run this or install the laravel password via composer so let's see composer require larabel password so it's installing the laravel passport package so it might take a while depending on your machine and the internet so it's already installed in my case so after installing the larabee passport we need to run this command so page purchase and passport install so what does page prts and password install do or command do so it basically it it generates a password request and grant key for the larabee password client so let's go ahead and run this command so as you can see it generates a an id and a secret for both access and grant after running the command we will go and navigate to the app and the user.php model and we need to add the class of the database passport so in this case we use larabel passport has epa tokens and at the same time we need to tell the larabel that we will be using the has api tokens so this is the larabelle class so basically it tells or it recognize they are able to check the api tokens so before the notifiable just type this as api tokens and we are good to go i forgot to mention that we we also need to register the passport routes into our larabel project so we can do that by going to the config sorry i up providers and app service provider oh no uh yeah so the old service provider we need to go here and we need to include the class so let's see how about passport passport then we will register to the the passport routes into the boot function so we can do that by writing this code routes so the laravel can now see the passport routes that we are we will be using [Music] so finally um the last step we need to do in order to run the reliable password is we need to change the config of the card so we need to initialize the api password api by going to config put the php so driver we need to change this to passport after completing the configuration we will now go and register our api routes for our project so in this case we will be creating a four routes so let's head over into routes and api.php so you can go here to the ros folder and click the api now you need to paste the following code for this tutorial uh we will just go and create four things so we have the login the sign up and log out and the user so as you can see we have the middleware wood api so if the current user is logged in so two functions key can be accessed or the route that he can access the logout and the user now we need to create a controller larabee makes it easy to create controller by running this command [Music] we can now navigate to the boot controller so in the app folder you navigate to http controllers now we need to include the user model so to do that after creating the controller so we need to add the following code so use carbon so the carbon package is to set the expiry token or expiration date using the carbon daytime package next is we will now create a sign up sign up function now i will create the validation using the validate built-in validate package so we were expecting two things basically basically three things on the name the email and the password so let's create a name and the requests surname you have the email request email and the password so here we will say it is required say required and for the password we need we also set it to required so the confirm so we specify the email as string required and we will also specify it is unique per users after validating the fields we will now create the user or save it so let's go and save it by using the code name you have the email and lastly the password so we will encrypt it via the encrypt bcrip function and then the request password for the login you basically need two things quality credentials and create user token so you can copy paste this code let me see we need to also set the log out function so here we use the logo request so we will do that by using the user token revo finally we display the current user the next thing we want to do now is to test if it is working so we run the page vrts and serve um and we will be using postman so postman is a request tester so you can download it so if you're using chrome you might want to use postman so here we use api would sign up suppose we create a name email password confirmation let's see if we will send call to undefined function button let's try again so yeah [Music] it creates a successful a new user so we can view so if we look at here there is api users now let's try the login using the pose method oh the post method um then the url is api with login so we need to set the body into raw data type json so we need to input the registered user valid so let's see for the login we can test it so so yeah so it says it is successful so we have the access token so the token type and the expires so expiry date is where the access token will be expired let's try the logout on the postman let's head over and go to log out copy paste the access token go to the log out barrier token paste it to the barrier token section and let's see where your top hand let's face it there you go successfully log up now we will go to show user so let's go to login login again copy paste the access token go to the authorization barrier token copy paste token that we are basing we we create a url called api with user so we set barrier token [Music] response i see so we need the arrow keys correct so let's send again let's try again there you go so i hope this tutorial helps you and if it's useful please subscribe to my channel and click the notification bell so you will be notified when i create a new tutorial or upload new tutorial i see you guys in next one bye [Music] is
Info
Channel: Coding Break
Views: 11,052
Rating: undefined out of 5
Keywords: Laravel Tutorial, REST API, Web Development, How to Create REST API, PHP, MySQL, API Development, Laravel Passport, Learning, Development, Programming, Programming Tutorial
Id: 0a0iABxTQuY
Channel Id: undefined
Length: 20min 46sec (1246 seconds)
Published: Wed Jul 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.