Laravel Sanctum

Featherweight token and SPA authentication for Laravel applications

Framework
Composer
vv4.3.3
2,988 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
72 /100 Good
Development Activity 56
Maintenance 56
Community 76
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
76 /100 Good
Architecture 80
Code Quality 82
Innovation 62
Learning Curve 78

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

PHP
82%
Other

Akaunting

Invoicing Finance

10,149

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
Built with
PHP 82%
Blade 13%
Updated 1 weeks ago
PHP
73%
MIT

Bagisto

Analytics · Ecommerce

28,186

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
67
Dependency
Built with
PHP 73%
Blade 21%
Updated 2 weeks ago
PHP
100%
Other

Cachet

Monitoring

15,251

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

View details
88
Repo Health
67
Technical
75
Dependency
Built with
PHP 100%
Updated 2 weeks ago
PHP
81%
Apache 2.0

Coolify

Devops · Hosting Control Panel

62,328

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
70
Dependency
Built with
PHP 81%
Blade 16%
Updated 1 weeks ago
PHP
48%
AGPL 3.0

Crater

Invoicing Finance

8,349

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
68
Dependency
Built with
PHP 48%
Vue 40%
Updated 2 years ago
PHP
82%
MIT

Databasement

Databases · Developer Tools · Devops

2,468

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
82
Repo Health
77
Technical
69
Dependency
Built with
PHP 82%
Blade 16%
Updated 1 weeks ago
Vue
54%
GPL 3.0

Financial Freedom

Invoicing Finance

2,927

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

View details
63
Repo Health
67
Technical
74
Dependency
Built with
Vue 54%
PHP 44%
Updated 2 weeks ago
PHP
58%
Other

Hi.Events

Ecommerce · Scheduling

4,037

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

View details
88
Repo Health
84
Technical
66
Dependency
Built with
PHP 58%
TypeScript 36%
Updated 2 weeks ago
PHP
51%
MIT

Krayin CRM

CRM · Ecommerce

23,954

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

View details
90
Repo Health
68
Technical
70
Dependency
Built with
PHP 51%
Blade 43%
Updated 2 weeks ago

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