Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Jwt Auth Laravel Package

php-open-source-saver/jwt-auth

Laravel JWT authentication package (fork of tymondesigns/jwt-auth) maintained by PHP Open Source Saver. Provides the same API with a new namespace, easy migration, and adds new features and ongoing support. Full docs available online.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Stateless Auth: JWT-auth aligns perfectly with Laravel’s stateless API-first architecture, eliminating session storage overhead. Ideal for microservices, SPAs, and mobile apps.
  • Guard Integration: Seamlessly integrates with Laravel’s built-in Auth::guard() system, supporting multi-guard setups (e.g., JWT + Session).
  • Event-Driven: Dispatches auth events (e.g., Authenticated, Attempting, Failed), enabling observability and custom logic (e.g., logging, analytics).
  • Token Blacklisting: Supports token revocation via blacklists, critical for security-sensitive applications (e.g., financial systems, admin panels).
  • Octane Compatibility: Optimized for Laravel Octane (high-performance async requests), reducing latency in real-time systems.

Integration Feasibility

  • Laravel Ecosystem: Designed for Laravel 10–13, with backward compatibility for 9.x via config tweaks. Leverages Laravel’s service container, events, and middleware.
  • Middleware Support: Provides jwt.auth middleware for route protection, replacing auth:api in traditional setups.
  • Customization: Extensible via:
    • Providers: Override JWTGuard or AbstractServiceProvider for custom token claims/validation.
    • Claims: Add/remove claims (e.g., roles, permissions) via payload configuration.
    • Token Storage: Supports database blacklists or Redis for scalability.
  • API-First: Built for REST/GraphQL APIs, with cookie-based auth for traditional web apps (via cookie_key_name config).

Technical Risk

Risk Area Severity Mitigation
Breaking Changes Medium Fork from tymon/jwt-auth introduces minor API shifts (e.g., JWTGuard constructor). Test thoroughly in staging.
Dependency Updates Low Dropped PHP 8.1 support; ensure CI/CD validates PHP 8.4+ compatibility.
Carbon Version Conflicts Low Supports Carbon 2/3; test with your app’s Carbon version.
Secret Key Management High Use Laravel’s env or Vault for secrets. Avoid hardcoding.
Token Revocation Latency Medium Redis blacklists reduce DB load; monitor getMinutesUntilExpired for performance.
Octane Edge Cases Low Test async request handling (e.g., token refresh races).

Key Questions

  1. Auth Flow Complexity:

    • Does the app require nested tokens (e.g., refresh tokens) or multi-factor auth? If yes, evaluate custom guard extensions.
    • Example: Auth::guard('api')->attempt($credentials, true) vs. manual JWT generation.
  2. Scalability Needs:

    • Will token blacklists be distributed (Redis) or centralized (DB)? Benchmark blacklist_add/isBlacklisted latency.
  3. Compliance:

    • Are there audit logs for token issuance/revocation? Extend JWTAuthEvent listeners.
    • Example: Log auth.attempted events to a SIEM.
  4. Legacy Migration:

    • If migrating from tymon/jwt-auth, test:
      • Auth::id() delegation (v2.6.0+).
      • Custom id claim fallback (v2.3.0+).
  5. Performance:

    • For high-throughput APIs, profile:
      • Token generation (Auth::login()).
      • Blacklist checks (Auth::invalidateBlacklistedTokens()).

Integration Approach

Stack Fit

  • Laravel Core: Native integration with Auth, Events, and Middleware.
  • PHP Versions: PHP 8.1+ (dropped 8.1 in v2.3.0; validate CI/CD).
  • Databases: Agnostic; blacklists work with MySQL, PostgreSQL, or Redis.
  • Caching: Redis recommended for blacklists (low-latency revocation).
  • Async: Octane-compatible; test with Laravel Horizon/Queues for background token cleanup.

Migration Path

  1. Dependency Swap:

    composer remove tymon/jwt-auth
    composer require php-open-source-saver/jwt-auth
    
    • Replace use Tymon\JWTAuth with use PHPOpenSourceSaver\JWTAuth.
  2. Config Updates:

    • Publish config: php artisan vendor:publish --provider="PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider".
    • Key changes:
      • show_blacklist_exception (default: true).
      • cookie_key_name (customize auth cookie name).
      • iat_unchanged_on_refresh (v2.8.0+).
  3. Guard Initialization:

    • Update JWTGuard constructor if autodiscovery is disabled:
      $guard = new JWTGuard($request, $app['auth'], $eventDispatcher);
      
  4. Middleware:

    • Replace auth:api with jwt.auth in app/Http/Kernel.php:
      'api' => \PHPOpenSourceSaver\JWTAuth\Middleware\GetUserFromToken::class,
      
  5. Testing:

    • Validate:
      • Token generation (Auth::login()).
      • Revocation (Auth::invalidateBlacklistedTokens()).
      • Edge cases (expired tokens, malformed payloads).

Compatibility

Component Compatibility
Laravel 13 ✅ Supported (v2.9.0+).
Laravel 12 ✅ Supported (v2.8.0+).
Laravel 11 ✅ Supported (v2.2.0+).
Lumen ✅ Partial (use LumenServiceProvider).
Carbon 2/3 ✅ Supported (v2.7.0+).
lcobucci/jwt 5.x ✅ Required (v2.4.0+; dropped 4.x).
Redis ✅ Recommended for blacklists.
Database Blacklists ✅ Supported (MySQL/PostgreSQL).

Sequencing

  1. Phase 1: Core Integration (1–2 sprints)

    • Swap dependencies, update config, test basic auth flows.
    • Prioritize: Login, logout, token validation.
  2. Phase 2: Advanced Features (1 sprint)

    • Implement blacklists (Redis/DB).
    • Add custom claims (e.g., roles).
    • Configure TTL per guard.
  3. Phase 3: Optimization (Ongoing)

    • Profile token generation/revocation.
    • Tune Redis blacklist TTLs.
    • Add monitoring for auth.attempted events.
  4. Phase 4: Security Hardening (1 sprint)

    • Rotate JWT secrets (php artisan jwt:secret).
    • Audit token claims for PII exposure.
    • Implement rate limiting on auth endpoints.

Operational Impact

Maintenance

  • Dependency Updates:
    • Monitor lcobucci/jwt and Laravel version compatibility.
    • CI/CD pipeline should test PHP 8.4+ and Laravel 13+.
  • Secret Rotation:
    • Use php artisan jwt:secret to regenerate keys.
    • Automate via cron (e.g., monthly rotation).
  • Blacklist Management:
    • Schedule cleanup for stale blacklisted tokens:
      // app/Console/Commands/CleanupBlacklist.php
      public function handle() {
          \PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth::invalidateOldBlacklistedTokens(now()->subDays(30));
      }
      

Support

  • Common Issues:
    • Token Expiry: Verify ttl config and iat claim handling.
    • Blacklist Failures: Check Redis connection or DB queries.
    • Carbon Errors: Ensure Carbon 3 compatibility (v2.7.0+).
  • Debugging Tools:
    • Log auth.attempted/auth.failed events.
    • Use dd(\PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth::getToken()) to inspect tokens.
  • Community:
    • GitHub issues are active; fork is maintained (vs. abandoned tymon/jwt-auth).

Scaling

  • Horizontal Scaling:
    • Stateless: JWTs enable stateless scaling; no session affinity needed.
    • Blacklists: Redis cluster for distributed revocation.
  • Performance Bottlenecks:
    • Token Generation: Offload to queues
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor