How to create API Authentication using Passport

In this article, We are going to explain “How to create API Authentication using passport in laravel”  application. In Laravel 5.2, we got a new structure in our authentication system with multiple auth drivers. This means that, instead of there being a single auth system that is responsible for one app at a time, you can apply different auth systems to different routes (or in different environments).

Web services

web service is very important for when you are create web and mobile apps.

If you require to create API for mobile application As we know laravel is most popular framework for API creation. If you are beginners and don’t know what is API and web services then you are a right place. In this article I will show how to create very simple API and Authentication for mobile APPs.



Laravel provide passport package for API authentication, In this example we will use passport package for user auth via API.

You have to just follow below given steps for users API.

  1. Register API
  2. Login API
  3. User Details API

Above three API, you can simply get by following few step. It is from scratch so just follow bellow steps, at last i attach screen shot of api test.

Step 1:- In first step require to install fresh laravel using below command, So open your terminal or command prompt and run command.

Step 2:-  In this  step to get install the passport package in laravel using below command

After passport installation package open config/app.php file and add service provider

Step 3:- The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after registering the provider. The Passport migrations will create the tables your application needs to store clients and access tokens.

Next, you should run the passport:install command. This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create “personal access” and “password grant” clients which will be used to generate access tokens.

Step 4:- In this step we have to configure passport, So just follow three given file changes.

firest we need to add the Laravel\Passport\HasApiTokens trait to your App\Usermodel. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user’s token and scopes.

App/User.php

app/Providers/AuthServiceProvider.php

config/auth.php

Step 5:- In this step, We will create API routes laravel provide api.php file for write web services routes So lets add new routes on that file.

routes/api.php

Step 6:- In this step  first we have create new directory API on controller folder  and create new controller in API directory, So let’s create UserController and put below code.

app/Http/Controllers/API/UserController.php

Now we are ready to run our API, So run below command

Now, we can simply test API using postman tool, I have test it and you can see screenshot.

Register:-

 

Login:-

 

getDetails:-

 

We hope this article is very helpful for creating API using passport for mobile application.

Thanks !!

Add a Comment

Your email address will not be published. Required fields are marked *