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.

Serving Laravel Project

  • 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.

Laravel Auth

  • 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

Leave a Comment