MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Create Rest API with Laravel Passport Authentication

Laravel passport

Laravel Passport is an OAuth2 server and API authentication package that is simple and enjoyable to use. We will use laravel passport to authenticate token-based request except for session

You have to just follow a few steps to get following web services:

  • Login API
  • Register API
  • Details API

Step 1 – First Create a Project

$ composer create-project --prefer-dist laravel/laravel RestAPI "5.8.*"
Code language: JavaScript (javascript)
Terminal running composer create-project to install Laravel 5.8 into the RestAPI folder
Composer install finishing with package discovery and application key set successfully

Step 2 – Install Package

There are two ways to install package:

  1. Write the following command given below
composer require laravel/passportCode language: JavaScript (javascript)

2. Update the composer and check your composer.json file

composer update
VS Code composer.json with laravel/passport ^7.5 listed in the require section

Step 3 – Open config/app.php file and add service provider

Config/app.php in VS Code with Laravel\Passport\PassportServiceProvider added to providers

Step 4 – Update database name (.env file)

.env file in VS Code with DB_DATABASE set to restapi

Step 5 – Open phpMyAdmin and create databse

XAMPP Control Panel with Apache and MySQL running and the MySQL Admin button for phpMyAdmin
PhpMyAdmin Databases tab creating a database named restapi to match the .env file

Step 5 – Run Migration and Install

php artisan migrate
Php artisan migrate output creating users, password resets and OAuth tables

php artisan passport:installCode language: CSS (css)
Php artisan passport:install output with encryption keys and personal access and password grant clients

Step – 6 Modify User.php

Go to app–>http–>User.php

User.php in VS Code with the HasApiTokens import and trait added

Step – 7 Modify app/Providers/AuthServiceProvider.php

AuthServiceProvider.php with the Passport import, policy uncommented and Passport::routes() added

Step 8 – Modify config/auth.php

Step -9 Create API Route

Go to routes–>api.php

Step 10 – Create The Controller

 php artisan make:controller UserController
Code language: CSS (css)
Php artisan make:controller UserController output showing Controller created successfully

Step 11 – Open UserController

  • We create an API folder inside the Controllers folder
  • After Creating, we Move UserController.php to API folder

Go and Run the server inside RestAPI folder, code is given below-

$php artisan serveCode language: PHP (php)

Step – 12 Download the app to quickly get started using the Postman API Platform. 

Google search for postman download with the Download Postman result link highlighted
Postman downloads page with the Windows 64-bit option of Download the App highlighted

Step – 13 After Installing, Open and Hit the API

Postman POST to /api/register returning 401 with name, email and password field is required errors

We have to write key-value pair to store data in database. Given Below

Postman register request with name, email and password params returning success and a Passport token

Now, Token is generated in the above image by hitting the send button. After hitting the send button the key-value pair is stored in the database. Let’s go and open phpMyAdmin given below.

PhpMyAdmin restapi users table showing the newly registered user row

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Creating a OAuth 2.0 Protected REST API In Laravel

Download and install WAMP or XAMPP Server Download and install composer Open Gitbash and Set the composer composer global require “laravel/installer” Create a new Laravel Project Refer…

Read More

Laravel: oauth_access_tokens clean up process

using Laravel Passport—the oauth_access_tokens table balloons when tokens aren’t expiring, not being revoked on logout, or not being purged. Here’s a practical, safe cleanup + prevention plan…

Read More

Laravel: Complete Guide of Laravel Passport

Here’s a comprehensive, up-to-date step-by-step tutorial for setting up and using Laravel Passport (OAuth2 API authentication) with clear explanations, working code samples, and troubleshooting tips—suitable for local…

Read More

artisan Tutorials: php artisan passport

When you run php artisan passport:install, Laravel attempts to do the following: Running php artisan passport:install is a crucial setup step when using Laravel Passport for API…

Read More

A Comprehensive Guide to API Authentication: Methods, Mechanisms, and Tools

1. Introduction APIs (Application Programming Interfaces) have become a cornerstone in modern software architecture, enabling communication between disparate systems and services. As APIs expose data and functionality…

Read More

How to Create Rest API with Laravel 5.5 Passport Authentication (Part 1)

Deepak Kumar MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in…

Read More