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.
Table of Contents
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/).
[crayon-662fb77e9b64e923358896/]
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.
[crayon-662fb77e9b657682325819/]
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
[crayon-662fb77e9b65a581892432/]
File: app/Providers/AuthServiceProvider.php
[crayon-662fb77e9b65c464923273/]
File: config/auth.php
[crayon-662fb77e9b65e610674835/]
Add Product Table and Model
First, we run the following command to perform the database migration process.
[crayon-662fb77e9b661586183163/]
After running this command, a file named “database/migrations” will be created in the project directory. Paste the codes below into this file
[crayon-662fb77e9b662284436138/]
After creating migration, we run the following command.
[crayon-662fb77e9b663060641383/]
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
[crayon-662fb77e9b665369893387/]
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
[crayon-662fb77e9b667527184547/]
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
[crayon-662fb77e9b670806229250/]
File: app/Http/Controllers/API/RegisterController.php
[crayon-662fb77e9b674035341752/]
File: app/Http/Controllers/API/ProductController.php
[crayon-662fb77e9b676582003793/]
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.
[crayon-662fb77e9b67a840485819/]
Now there is the created new file with a new folder on the following path:
File: app/Http/Resources/Product.php
[crayon-662fb77e9b67e916134637/]
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.
[crayon-662fb77e9b67f968271614/]
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
Login API: Verb:GET, URL:http://localhost:8000/api/login
Product Create API: Verb: POST, URL:http://localhost:8000/api/products
Product Show API: Verb: GET, URL:http://localhost:8000/api/products/{id}
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.