Laravel 9 User Registration Login Api with Passport Authentication

In this tutorial you will learn about the Laravel 9 User Registration Login Api with Passport Authentication and its application with practical example.

In this Laravel 9 User Registration Login Api with Passport Authentication Tutorial I will show you how to create user authentication(registration and login) REST API with passport authentication In laravel. In this tutorial you will learn to create authentication rest api with passport authentication In laravel 9 application. In this article I will share example to create rest api for user authentication using passport authentication in laravel 9. I will also show you how to install passport authentication package in laravel 9. After installing and configure passport authentication in laravel we will create simple user authentication rest api.

Laravel 9 User Authentication API

By user authentication means to validation and authentication valid application users. User authentication allows to register new user into application and to login existing application users. In this tutorial we will be creating user authentication api means we will be creating rest api for user registration, login and to get user info. We will be using passport authentication for creating rest api for user authentication in this example laravel 9 application.

Laravel 9 User Registration Login Api with Passport Authentication

In this step by step tutorial I will guide you through create a fully functional restful API with passport authentication in Laravel 9. Please follow the instruction given below:

  1. Install Laravel 9
  2. Set Up Database
  3. Install Passport Package
  4. Configure Passport Module
  5. Create Post Model & Run Migration
  6. Create a New Controller
  7. Define API Routes
  8. Test Laravel Passport API

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Configure Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Passport Package

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

After successfully install laravel passport, register providers. Open config/app.php . Put the bellow code in it:

config/app.php

Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.

Passport Configuration

Open User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

app/Models/User.php

go to  app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function.

config/auth.php

Run Migration

Now, run the migration to create database table using following artisan command:

Create Passport Auth Controller

Now, lets create a controller named PassportAuthController using command given below –

Once the above command executed, it will create a controller file PassportAuthController.php in app/Http/Controllers/Api/ directory. Open the PassportAuthController.php file and put the following code in it.

app/Http/Controllers/Api/PassportAuthController.php

Define API Routes

Now we will define authentication rest API routes. Go to the routes directory and open api.php. Then put the following routes into api.php file:

routes/api.php

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Laravel 9 User Registration API:

Laravel 9 User Login API:

Laravel 9 Get User Info. API:


You need to set this access token as a Bearer Token in the Authorization header.

In this tutorial we have learn about the Laravel 9 User Registration Login Api with Passport Authentication and its application with practical example. I hope you will like this tutorial.