Skip to main content

Simplify Your Tax Calculations with GST Calculator 2024: The Ultimate Tool for Businesses and Individuals

Title: Understanding Laravel Passport Authentication System: A Comprehensive Guide with Code Examples

Laravel, one of the most popular PHP frameworks, provides a robust authentication system known as Laravel Passport. Passport simplifies the implementation of OAuth2 server capabilities, making it easy to secure your API routes and control access to your application. In this blog post, we'll explore the fundamentals of Laravel Passport authentication and guide you through the process with detailed code examples.


Before we dive into Laravel Passport, ensure you have a Laravel application up and running. If you haven't already installed Laravel, you can do so using Composer:

composer create-project --prefer-dist laravel/laravel your-project-name

Once your Laravel project is ready, let's begin by installing Laravel Passport.

Step 1: Install Passport:

Use the following Composer command to install Laravel Passport:

composer require laravel/passport



After installation, run the migration command to create the necessary tables in your database:


php artisan migrate

Step 2: Configure Passport:

Next, you'll need to add the Passport service provider and run the Passport install command:
php artisan passport:install

This command generates encryption keys and other necessary configurations for Passport.

Step 3: Update User Model:

In your User model (typically located at app\Models\User.php), add the HasApiTokens trait:
1
2
3
4
5
6
7
8
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    // Rest of your user model code...
}

Step 4: API Routes:

Define the routes that you want to protect using Passport in the routes/api.php file:
1
2
3
Route::middleware('auth:api')->group(function () {
    // Your protected API routes go here
});

Step 5: Passport Middleware:

Ensure your App\Http\Kernel.php file includes the \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class middleware:

1
2
3
4
5
6
7
protected $middlewareGroups = [
    'web' => [
        // ...
        \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
    ],
    // ...
];

Step 6: Passport Configuration:

Customize your Passport configuration in the config/auth.php file. Set the driver option in the api guard to passport:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Conclusion:

Congratulations! You've successfully set up Laravel Passport for authentication in your Laravel application. Passport makes it easy to secure your API routes and control access to your application by providing a powerful OAuth2 server out of the box. Explore more Passport features, such as scopes and client management, to enhance the security and functionality of your Laravel API. Happy coding!

Comments

Popular posts from this blog

Create Any Recyclerview Adapter within 5 seconds, in Android | Kotlin.

 In every single android application, we need to make many number of recycler-view adapter. So every time you create a blank adapter and modify it as per your requirements. But it is very boring and as well as time taking. So here I give you a solution that you can create any recyclerview adapter within 5 seconds using file template in Android Studio. So, Lets read this full article- First we open android studio and create a project. After project creation  Open File->new->Edit File Template... Follow this below screenshot Okay, After click on "Edit File Template" a popup is open "File and Code Template", Now click on the  " + " icon and set set your template name. Now set your template name, template extension and template body. after doing everything click on "Apply" and then click on "OK" The template body I after after the screenshot Template Body Copy this code and paste it on the template body section, which I marked in scre...

How to fetch Latitude, Longitude from address and vice-versa(address from Latitude, Longitude) using Google Geo coder SDK in android| Kotlin

 In this Android development related article, you will get a simple solution that, how to get address using Latitude, Longitude and vice-versa. i.e latitude, longitude from an address text. It is very easy and simple. Read full article and carefully follow all the steps. Here we use google Geocoder SDK. Okay, first we create an android project in kotlin and create an Activity say MainActivity.kt. Use the below code- Function get Latitude, Longitude from Address- fun getLatLngFromAddress (context: Context, mAddress: String): String { val coder = Geocoder(context) lateinit var address: List<Address> try { address = coder.getFromLocationName(mAddress, 5 ) if (address == null ) { return "Fail to find Lat,Lng" } val location = address[ 0 ] return " Latitude: ${location.latitude}\n Longitude: ${location.longitude}" } catch (e: Exception...

Recycler View Like Google Play Store App | How to use SnapHelper Android, Kotlin

 If we see the google play store android app, we can see that the app list in horizontal recycler view hold a property that the first property hold always full visible or not visible, but a portion visible is not seeing. This property is snap property. In this article I show you how to use it in our own application. Basically we use two type of snap. Center Snap and start snap. I show you both here. So read full article here. The Key Moment code is here- For center snap you need to write 2 lines code- //center Snap val snapHelper = LinearSnapHelper() snapHelper.attachToRecyclerView(recyclerView1) For start snap, create a class in kotlin say StartSnapHelper.kt package com.example.snaphelpersampleapp import android.view.View import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearSnapHelper import androidx.recyclerview.widget.OrientationHelper import androidx.recyclerview.widget.RecyclerView class StartSnapH...