Web Prepration

Web Concept Tutorials

CodeIgnitor, Laravel, PHP

In this post, I am going to explain “how to create rest api with authentication using passport” in laravel 6 application. Rest API is must be used when you are working with mobile application. when your application is preferred for a web app and the mobile app then you must have to create API for your mobile app development.

However, Laravel provides an easy way to create Rest API using the passport package. if you need to authentication in your mobile app then you can easily do it using a passport. Laravel 6 Passport provides a way to create an auth token for validating users.

Web services

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

How to Create Rest API with Authentication Using Passport

If you require to create API for mobile application As we know laravel is the most popular framework for API creation. If you are beginners and don’t know what is API and web services then you are the right place. In this article I will show how to create API and Authentication for mobile APPs.i will show you step by step build restful API authentication using eloquent API resources in laravel 6.

Follow bellow a few steps to create a restful API example in laravel 6 app.

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

Above three API, you can simply get by following a few steps. It is from scratch so just follow the below steps, at last, I attach a screenshot of the API test.

Step 1:-  In the first step required 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 LaravelPassportHasApiTokens trait to your AppUsermodel. 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.

Read Also: Laravel Passport Auth

Register:-

 

Login:-

 

getDetails:-

 

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

Thanks !!

0 comment on Laravel 6 REST API Using Passport Package

Write a comment

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