Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APIAutomation

Create REST API in Laravel with authentication using Passport

In this article, while developing Rest API with Laravel Framework, we will authenticate with Laravel Passport, the official package of Laravel.

The concept of authentication has a very important place in application development processes. Different programming languages contain different solutions and approaches in themselves. In this article, while developing Rest API with Laravel Framework, we will authenticate with Laravel Passport, the official package of Laravel.

What is Laravel

Laravel is a PHP framework used for developing web applications. Laravel includes many advanced features and has many useful features of PHP and OOP. Laravel’s motto, which is used by a wide audience, is: “Web Artists’ PHP Framework”. You can access the official document from this link: https://laravel.com/.

Why Laravel

It is preferred by many developers because it keeps pace with the rapid change of Software Technology and offers us software principles and saves us from most of the manual work (Authentication, Middleware, ORM, MVC) that is required for project development to make it fast, reliable and testable.

What is Laravel Passport

It is the official Laravel package that facilitates authentication in Rest APIs and is written with Laravel.

Creating a Boilerplate Application

In this section, we will develop a Rest API application in Laravel step by step and look at the use of Passport.

Install Laravel

First of all, we install Laravel Framework on our computer via Composer. If you do not have Composer on your computer, you can download it from this link (https://getcomposer.org/).

Install Passport

We will install Passport on our computer through Composer. Then we will perform the default setup of the database and tokens with the last two commands.

Configure Passport

In this step, we will set up Passport settings. We create three different php extension files and paste the following codes.

File: app/User.php

File: app/Providers/AuthServiceProvider.php

File: config/auth.php

Add Product Table and Model

First, we run the following command to perform the database migration process.

After running this command, a file named “database/migrations” will be created in the project directory. Paste the codes below into this file

After creating migration, we run the following command.

We created the “Product” table and now we will create our product model. We create a file named “Product.php” in the project directory and paste the following codes.

File: app/Product.php

Create API Routes

In this step, we will create API routes. Laravel provides an api.php file for writing web services routes. So, let’s add a new route to that file.

File: routes/api.php

Create Controller Files

In this step, we will create three Controllers as BaseController, ProductController, and RegisterController, where we will create our Rest APIs. Paste the codes below.

File: app/Http/Controllers/API/BaseController.php

File: app/Http/Controllers/API/RegisterController.php

File: app/Http/Controllers/API/ProductController.php

Create Eloquent API Resources

Eloquent API will help us to make the same response layout as your model object. We used it in the ProductController file.

Now there is the created new file with a new folder on the following path:

File: app/Http/Resources/Product.php

Test Application

To test our application, we will first run it in a local environment with the command below. Then we will test our Restful APIs through Postman.

After running the application, we perform our tests with Postman. You can view the screenshots.

Register API: Verb:GET, URL:http://localhost:8000/api/register

postman register endpoint

Login API: Verb:GET, URL:http://localhost:8000/api/login

postman login endpoint

Product Create API: Verb: POST, URL:http://localhost:8000/api/products

postman products endpoint

Product Show API: Verb: GET, URL:http://localhost:8000/api/products/{id}

postman product endpoint

Conclusion

In this article, we made our Rest APIs more secure by using Passport with Laravel very quickly with the speed factor, which is the biggest feature that Laravel provides to developers. Hope this information will be useful.

Related posts
APICurrency

Exchange Rate API Integration in E-Commerce App in 2024

APICurrency

Building a Real-Time Currency Converter in Java Using Currencylayer API

API

10 Best Stocks APIs For Developers

API

Top 10 Flight Search APIs in 2024

Leave a Reply

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