Laravel 5 API Authentication Using Passport

In this post, I’m going to share how to build REST API web services authentication in Laravel 5 application using API Passport package. Nowadays Web services are very necessary while we are creating Web and Mobile application developing for multiple platform purpose. We need to create API for our mobile application developer. We understand Laravel is very popular and popular because of secure API. Laravel provides Passport package for secure API authentication. Passport package provides OAuth2, JWT authentication to access the web server using API.

Most of the single page applications (or SPAs) enhancing very familiar and very quickly we see an increasing want to attach APIs for every process in it. Most organizations use the first step by building a simple and quick REST API. I write Laravel script that makes a simple and quick REST API from your MySQL Database tables with login and register with user list view API services.

Step 1: Install Laravel

Here, we need new Laravel application for API passport implementation and demonstration. the following command helps to create a new application.

Step 2: Install Package

In this step, we have to download laravel/passport package goto your command and run the following command

Step 3: Make configuration for application

This step, we need to configure package provider in config/app.php file and add following service provider.

Step 4: Run Migration and Install Passport

After registering Passport service provider, we need to run the migrations and it will create new tables in our database.

Step 5: Install Passport Package

This step, we want to download and install Laravel Passport package using the following command, it will install and create new token keys for security in a database.

Step 6: Passport Configuration

Here, we should configuration in three place Model, Service provider, and Auth config file. In model, we need to include HasApiTokens class of Passport and use HasApiTokens in our model.

Then we need to include passport class and add routes “Passport::routes()” in boot method in AuthServiceProvider.

and also, we need to modify API driver details with passport and provider as users in auth.php.

Step 7: Create API Route in routes/api.php

Here we will add new API routes to access the laravel application and following routes are help us to make web service using a passport.

Step 8: Create Controller & Methods

In this step, we have to add a new user controller and three methods for API request handler, following code help us to make API access in our application.

 

Learn Infinity

Learn Infinity is the most famous Programming & Web Development blog. Our principal is to provide the best online tutorial on web development. We execute the best tutorials for web experts this will help developers, programmers, freelancers and proving free resource you can download or preview the tutorials.

Leave a Reply

Your email address will not be published.

Back to top