Laravel & Backend

Authentication & Authorization Patterns in Laravel: Sanctum, JWT & RBAC

By Joseph Magdy Habib | 2026-04-05 | 8 min read

Table of Contents

  1. 1. Choosing Between Session & Token Auth
  2. 2. Laravel Sanctum for SPA Authentication
  3. 3. JWT Tokens for API Consumers
  4. 4. Role-Based Access Control (RBAC)
  5. 5. Policy Classes & Resource Authorization
  6. 6. Protecting Against Common Auth Attacks

1. Choosing Between Session & Token Auth

Session-based auth (Sanctum cookies) is ideal for same-origin SPAs. Stateless JWT is preferred for mobile apps or cross-domain API consumers. Evaluate the threat model before choosing.

2. Laravel Sanctum for SPA Authentication

Sanctum issues CSRF-protected session cookies for first-party SPAs. Configure `SANCTUM_STATEFUL_DOMAINS` to match your frontend's origin. Never skip CSRF middleware for POST/PUT/DELETE routes.

3. JWT Tokens for API Consumers

Use `tymon/jwt-auth` for stateless token authentication. Store tokens in HTTP-only cookies on web clients, never in `localStorage`, to prevent XSS token theft.

4. Role-Based Access Control (RBAC)

Implement roles and permissions using a pivot table (`role_user`, `permission_role`) pattern or Spatie's `laravel-permission` package. Gate checks with `$this->authorize('action', Model::class)` in controllers.

5. Policy Classes & Resource Authorization

Create model-specific Policy classes and bind them in `AuthServiceProvider`. Policies centralize authorization logic and keep it out of controllers.

6. Protecting Against Common Auth Attacks

Rate limit login endpoints, enforce bcrypt cost factor ≥ 12, validate password reset tokens with time-limited signed URLs, and rotate sanctum tokens after privilege escalation.

Related Engineering & Architecture Guides

  • Architecting Scalable RESTful APIs with Laravel & Clean Architecture (6 min read)
  • Optimizing MySQL Database Performance & Query Execution in High-Load Systems (7 min read)
  • Laravel Best Practices: Building Secure, Scalable, and Maintainable Applications (7 min read)

← Back to All Articles | Explore Enterprise Projects | Contact Joseph Magdy