

Laravel REST API Authentication using Passport
source link: https://www.simplifiedcoding.net/laravel-rest-api-authentication/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Laravel REST API Authentication using Passport
Do you want to know about a proper token based authentication mechanism for your RESTful APIs? If yes, then you are at the right place. In this post we will learn about Laravel REST API Authentication using Passport.
We have built RESTful APIs, many time earlier, but we never discussed about proper AUTHENTICATION. And that is why I am writing this post.
In this post, we will create a RESTful API for Login and Signup using Laravel. So now, without wasting any more time, let’s start.
Laravel REST API Authentication using Passport – Video
If you are more comfortable in watching video tutorials rather than reading a long post, then don’t worry; I have a complete step by step video playlist for this topic as well.
But if you are ok with reading the post, then let’s move ahead.
Setting Up Everything
Before getting started we need to setup our development environment. So here are the things that I will be using.
- XAMPP (You can use other tools e.g mamp, wamp as well).
- Composer (Get it by clicking on the link)
- NodeJS
- Visual Studio Code (Or basically any code editor program).
Once you have all the above mentioned things, you are good to go.
Creating a Laravel Project
Now, choose a location in you machine where you want to save your project. Open terminal or command prompt here.
- We will run the following command to create a laravel project.
- In the above command MyAPIProject is the name of our project. You can change it if you want.
Launching the Application
- Our empty project is ready and now we can launch it locally.
- Go inside your project directory and run the following command.

- If you will go to the shown address in your browser, then you will see your laravel home page.
Setting Up Database
Once you have the project created, it is needed that you setup a database for your application.
- Go to PhpMyAdmin (localhost/phpmyadmin) and create a database.
- Now open the .env file that you have inside your project folder and make the following changes.
- You need to define values for DB_DATABASE, DB_USER_NAME, DB_PASSWORD and if you are using linux based system then you must define a new value DB_SOCKET as you can see above.
Creating Authentication
With Laravel it is extremely easy to make authentication, everything is already done and we just need to execute some commands.
- First we need the ui package of laravel.
- Then we will create auth with this command.
- After running the above command you will see the Login and Register button in your home page.

- Still we need some more commands to make everything work.
- First we will install all the required node packages.
- After installing all required node packages, run the following command.
- And finally we will migrate the database, using the following command.
- Now our authentication is done and we can do signup and login in our app.
Adding Laravel Passport
- Now let’s first require laravel passport that we will be using for our APIs.
- Again migrate the database using
- Now we need to generate required keys for passport.
Configuring Passport
- Open your User model class and add the following lines.
- Now we need to call Passport::routes() in our \App\Providers\AuthServiceProvider.php class.
- Now come inside config/auth.php file and here we will define passport as the driver for our apis.
Now passport configurations are done, and we can create our API routes. But first we will create an AuthController.
Creating Auth Controller
- First run the following command.
- Now go inside AuthController.php that is generated and write the following code.
- Now let’s create our API routes. So open routes/api.php and write the following codes.
- And you are done. You can test your APIs using POSTMAN or any other REST Client.
Deploying the Project to Server
Now if you want to deploy your project to a live server then it is also very easy. Check this video how to do it.
If you need detailed explanation then you should check all the video tutorials.
Laravel REST API Authentication Source Code
Finally if you need my source code then you can get it from here.
That is all for this tutorial friends. If you are having any problem or confusion about this Laravel REST API Authentication then feel free to comment it below. Thank You
Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.
Recommend
-
13
REST API and Passport Authentication with Laravel REpresentational State Transfer or REST is an architectural style for providing standards between computer systems on the web. We need REST API’s to communicate over various medium...
-
7
Today we will learn how to use the scopes and permission with API. In our example, we will create 3 different roles (admin, moderator, basic) with different permissions (view, add, edit, and delete). We will use the scope to moderate all the...
-
25
Technical Articles
-
8
Laravel 8 REST API Authentication using Sanctum Sep 15 ・3 min read ...
-
11
User Authentication in NodeJS Using Passport and MongoDB By Mary Gathoni Published 9 hours ago User authentication is c...
-
12
API Authentication in Laravel without Using Passport. 21094 views 2 years ago Laravel Since Laravel 5.3, Laravel Pa...
-
5
Paypal Card Payment REST API integration in Laravel 6 28436 views 2 years ago Laravel In this article we will share with you...
-
4
How to Create REST API in Laravel7 using Passport 33369 views 2 years ago Laravel Hey Artisan Hope you are doing well....
-
5
NodeJS User Authentication in NodeJS using Passport.js (local strategy) By
-
13
Restful API In Laravel 5.5 Using jwt Authentication 440980 views 5 years ago Laravel Today, we are share with you how to bui...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK