Menu

JWT Token क्या है? पूरी जानकारी, Use Cases, और Laravel में इसका इस्तेमाल

JWT Token क्या है? | JWT Authentication Use Case & Laravel Guide

JWT Token क्या है और यह API authentication में कैसे काम करता है? जानिए इसके फायदे, उपयोग, और Laravel में JWT को implement करने का आसान तरीका।

JWT (JSON Web Token) एक secure authentication method है जिसे modern APIs, मोबाइल ऐप्स और backend systems में user को verify करने के लिए इस्तेमाल किया जाता है। यह stateless, fast और cross-platform friendly होता है, इसलिए Laravel और अन्य frameworks में बहुत popular है।


Install JWT Token : composer require tymon/jwt-auth

Publish JWT Config : php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider

Auto generate config/jwt.php file

Generate JWT Secret : php artisan jwt:secret

Add JWT key in .env file
JWT_SECRET=your-generated-secret

Setup JWT in User Model : App\Models\User.php
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
}


Set JWT Driver on Auth Guard : config/auth.php
'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],


Create API Routes : routes/api.php
Example 
Route::middleware('auth:api')->group(function () {
    Route::get('profile', [AuthController::class, 'profile']);
    Route::post('logout', [AuthController::class, 'logout']);
    Route::post('refresh', [AuthController::class, 'refresh']);
});

Flow-chart laravel

Contact