What is REST API?
Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the Internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.
source: wikipedia
Laravel 9 REST API with Passport Authentication Tutorial
In Laravel, you can take the holistic approach to build API. You won’t have to put intensive efforts; instead, you can give precedence to security. As far as security is concerned, Laravel Passport takes care of security and allows you to create Auth Token to provide authentication to users.
In this tutorial, we will learn to create robust, fast, and secure CRUD (CREATE, READ, UPDATE, DELETE) RESTful Authentication API with Passport Package in Laravel by following all the imperatives needed to be followed.
What is API (Application Programming Interface)?
API refers to the Application Programming Interface. It is a set of routines, protocols, and tools for creating software applications. An API interface makes communication possible between various software components.
In software development, API is a URL that handles the data for the web application through HTTP Requests GET, POST, UPDATE & DELETE, and manages the CRUD operations.
source: positronx
What is REST API?
Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the Internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.
source: wikipedia
Install New Laravel Project
Let’s invoke the following command in the terminal to install a brand new Laravel application.
Set Up Database
This step explains how to make consensus between laravel and database, Incorporate the following code in .env
file to establish the connection between both parties.
If you are using MAMPP, then you might get the given below error while running migration. Please add the following line of code right after your database configuration inside the .env file.
Install Passport Package
On an impulse, the second step leads us to install the passport package through Composer package manager. Without further ado run the following command in your terminal.
Ideally, we have to use the default migration to create a new table in the MySQL database.
Next, generate token keys for strengthening the security and restrain hackers from deteriorating the security of our applications.
Configure Passport Module
We need to focus on some nitty-gritty to configure the Passport package in the Laravel application. First, open app/Models/User.php file and include HasApiTokens trait inside the User model, as mentioned below.
Next, open app/Providers/AuthServiceProvider.php file and register the registerPolicies()
method inside the boot()
function, It will evoke the required routes.
Register the PassportServiceProvider class in providers array inside the config/app.php file:
Configure driver for the Passport, get inside the config/auth.php file and make the changes as shown below.
Create Posts Model & Run Migration
To make the consensus between client and server, we will have to create the Post model by executing the below command.
After executing the above command, you will see the archetype of posts migration file in database/migrations/timestamp_create_posts_table. Here, you have to add some values to create the internal coherence using Model.
Next, create the app/Models/Post.php file and register the following values inside the $fillable
array.
Then, run the migration by using the below command.
Create a New Controller
Let us take another imperative in the consideration and, on the same impetus, execute the following command. It will create a new controller in our laravel app to create a login and registration REST API.
Controller is the quintessential file in Laravel application development. So, without further insert the given below code in PassportAuthController.php file.
Before we move to next step, establish consensus between Post and User model. Gradually incorporate the following method inside the app/Models/User.php file.
Run command to create Post Controller.
Add the following code in PostController.php file.
Define API Routes
Now, we will define API routes. Go to routes/api.php file and declare the foundational code.
Test Laravel 9 Passport API
Eventually, we have completed all the foundational steps that were required to build REST API with Passport authentication in Laravel. Now, the time has come to test out the API, so run the following command to start the laravel app.
We have to rely on Postman for testing our newly formed endpoints.
Register API:
You can test the Laravel Passport API for registering the user:
Please open the Postman app and Headers tab, define "Accept": application/json
header value:
Login Passport API:
After sign up, copy the Bearer token, set into the Headers section in the Postman app. Check out the Laravel Passport Endpoint for logging-in:
Passport Post Create API:
To perform the CRUD operation, we need to set the correct authenticity. After successful registration and login, you will receive the access token. The manifestation of access token creates coherence with authorization, and It establishes secure communication with the server. You need to set this access token as a Bearer Token in the Authorization header.
Post List API:
Show Single Post API:
Post Update API:
Post Delete API:
The Bottom Line
Eventually, we have completed the Laravel 9 Passport API Tutorial. In this tutorial, we have shed light on every aspect needed to build secure REST APIs in Laravel.
We have gone through every foundation step and put everything at its place without falling into the trap of procrastination. This tutorial is useful for those who are new and want to try their hands to create a secure REST API with Passport in Laravel.
I have tried to shape things from my outlook on the entire journey, i haven’t been skeptical about anything. Anyhow, If i have skipped anything due to recklessness, you must download the full code of this tutorial from the GitHub.
0 comments:
Post a Comment
Thanks