Laravel Sanctum

Featherweight token and SPA authentication for Laravel applications

Framework
Composer
vv4.3.3
2,976stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
80/100Excellent
Development Activity68
Maintenance76
Community76
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
76/100Good
Architecture80
Code Quality82
Innovation62
Learning Curve78

Laravel Sanctum is the official, lightweight authentication package for Laravel applications that need to authenticate single-page apps, mobile apps, or simple token-based APIs, without the complexity of a full OAuth server like Passport. It offers two authentication strategies from one package: stateful, cookie-based authentication for first-party SPAs sharing a session with the backend, and personal access tokens for third-party clients and mobile apps.

Sanctum integrates directly into Laravel’s authentication and middleware pipeline via the HasApiTokens trait and a dedicated Guard, so authenticating a request in a controller looks identical whether the caller used a session cookie or a bearer token. Tokens can be scoped with abilities (similar to OAuth scopes) to limit what actions a given token is allowed to perform.

What You Get

  • The HasApiTokens trait, adding createToken(), tokens(), and ability-checking methods to any Eloquent user model
  • A PersonalAccessToken model and migration for storing hashed API tokens with optional expiration and ability scopes
  • A stateful SPA authentication mode that uses Laravel’s existing session/cookie guard for first-party frontends on the same top-level domain
  • A custom Guard class wiring both token and session authentication into Laravel’s auth() facade transparently
  • Middleware (EnsureFrontendRequestsAreStateful) that marks requests from configured frontend domains as session-authenticated instead of token-authenticated
  • Console commands (sanctum:prune-expired) for cleaning up expired tokens on a schedule

Common Use Cases

  • Authenticating a same-origin Vue/React/Inertia SPA against a Laravel backend using session cookies instead of tokens
  • Issuing scoped personal access tokens to mobile apps or third-party API consumers
  • Restricting what a given API token can do via named abilities (e.g. token->can('post:create'))
  • Building a simple public API for a Laravel app without standing up a full OAuth2 server
  • Pruning expired or unused tokens automatically via a scheduled console command

Under The Hood

Architecture: SanctumServiceProvider registers the package’s config, migrations, and the custom Guard (in src/Guard.php), which is invoked per-request to resolve the authenticated user — first checking configured session guards, then falling back to bearer-token lookup via getTokenFromRequest(). HasApiTokens.php is the trait consuming applications mix into their User model, exposing createToken() (returns a NewAccessToken pairing the plaintext token with its hashed PersonalAccessToken record) and ability-checking helpers. Http/Middleware/EnsureFrontendRequestsAreStateful.php flags requests from allowlisted frontend domains so the guard treats them as session-authenticated SPA traffic rather than token traffic.

Tech Stack: Pure PHP, built entirely on Laravel’s own framework contracts (Illuminate\Contracts\Auth, Eloquent) — it has no dependencies outside the Laravel ecosystem itself, since it’s designed to be installed directly into a Laravel application via composer require laravel/sanctum and its own service provider.

Code Quality: The tests/ directory (Feature and Unit suites, 14+ test files) exercises the guard’s session-vs-token resolution paths, token ability scoping, and console commands, run via Orchestra Testbench for isolated Laravel package testing. The codebase follows Laravel’s own coding conventions closely (PHPDoc blocks, PSR-style namespacing) as an official first-party package maintained by the Laravel core team.

API Design: The core ergonomic win is that auth()->user() and route middleware work identically regardless of whether the request came in with a session cookie or a bearer token — consuming applications write one auth check, and Sanctum resolves which mechanism applies underneath, which is what makes it ‘featherweight’ relative to configuring a full OAuth2 flow for the same SPA-plus-mobile-client use case.

Used by 13 apps in this directory

Other

Akaunting

Invoicing Finance

10,070

Open-source online accounting for small businesses and freelancers — invoices, expenses, and reporting without monthly fees or vendor lock-in.

View details
98
Repo Health
73
Technical
63
Dependency
Updated yesterday
PHP
72%
MIT

Bagisto

Analytics · Ecommerce

27,983

Open-source Laravel eCommerce platform for building multi-vendor marketplaces, B2B stores, headless commerce, and AI-powered storefronts.

View details
93
Repo Health
75
Technical
69
Dependency
Built with
PHP72%
Blade22%
Updated today
PHP
100%
Other

Cachet

Monitoring

15,220

The open-source, self-hosted status page system that keeps your users informed during outages and maintenance windows.

View details
86
Repo Health
67
Technical
76
Dependency
Built with
PHP100%
Updated 2 days ago
PHP
79%
Apache 2.0

Coolify

Devops · Hosting Control Panel

60,738

Open-source self-hosted PaaS — deploy apps, databases and 280+ services on your own servers with no vendor lock-in

View details
92
Repo Health
86
Technical
72
Dependency
Built with
PHP79%
Blade18%
Updated yesterday
PHP
48%
AGPL 3.0

Crater

Invoicing Finance

8,341

Open source invoicing and billing platform for freelancers and small businesses — create estimates, track expenses, accept Stripe payments, and run recurring invoices from your own server.

View details
54
Repo Health
68
Technical
69
Dependency
Built with
PHP48%
Vue40%
Updated 2 years ago
PHP
82%
MIT

Databasement

Databases · Devops · Developer Tools

1,974

Self-hosted database backup manager with a web UI for scheduling, encrypting, and restoring MySQL, PostgreSQL, MongoDB, SQL Server, SQLite, Firebird, and Redis to S3, SFTP, Samba, or local storage.

View details
81
Repo Health
77
Technical
69
Dependency
Built with
PHP82%
Blade17%
Updated 4 days ago
Vue
54%
GPL 3.0

Financial Freedom

Invoicing Finance

2,910

Own your financial data with a self-hosted, privacy-first budgeting app that replaces Mint and YNAB.

View details
49
Repo Health
67
Technical
75
Dependency
Built with
Vue54%
PHP44%
Updated 3 months ago
PHP
57%
Other

Hi.Events

Ecommerce · Scheduling

3,985

Self-hosted event ticketing platform that keeps your attendee data, brand, and revenue completely under your control.

View details
88
Repo Health
84
Technical
67
Dependency
Built with
PHP57%
TypeScript37%
Updated 6 days ago
PHP
51%
MIT

Krayin CRM

Ecommerce · CRM

23,694

Free, open-source Laravel & Vue.js CRM for SMEs and enterprises to manage the complete customer lifecycle.

View details
88
Repo Health
68
Technical
70
Dependency
Built with
PHP51%
Blade46%
Updated yesterday

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search