agentsclimarketplace

Laravel auth

Skill gab3mioni/laravel-engineering-skills/skills/laravel-auth

Authentication and authorization in Laravel 12 — Sanctum (SPA cookies + API tokens), Fortify, Breeze, Passport, guards, middleware, Policies/Gates, hashing. Use when choosing Sanctum vs Passport vs Fortify, wiring SPA cookie auth, designing token abilities, protecting routes, writing/reviewing Policies, or auditing auth in a PR. Symptoms — "419 on login from SPA", "logged out on every request", "user logged out after deploy", "the authenticated user is null in a queued job", "token-protected route returns 401 with a valid token". Used by shared Laravel roles.From its SKILL.md

Install
npx -y skills add gab3mioni/laravel-engineering-skills --skill laravel-auth

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

24.7 KB, as published. Nobody here has run it

Laravel Auth — Authentication & authorization

Identity and access for Laravel 12. Covers who you are (authentication: Sanctum, Fortify, Breeze, sessions, tokens) and what you can do (authorization: Policies, Gates, middleware). Stack-neutral on the frontend — examples that mention Inertia/SPA delegate UI specifics to the laravel-react/laravel-vue agents.

When to use this skill

  • Choosing between Sanctum SPA mode, Sanctum tokens, Fortify, Breeze, or Passport
  • Wiring a new auth flow (login, register, password reset, email verification, 2FA)
  • Designing token abilities/scopes for an API
  • Protecting routes with the right middleware (auth, auth:sanctum, verified, password.confirm, throttle)
  • Writing or reviewing Policies and Gates
  • Auditing auth-related code in PRs
  • Debugging "logged out on first request", CSRF/CORS issues, stale Sanctum cookies

When NOT to use

TopicUse instead
OWASP Top 10, password hashing internals, dependency CVEs, rate-limit headers, 2FA threat modellaravel-security skill
Eloquent for User, FormRequests for login, controller patternslaravel-backend skill
Inertia shared auth.user prop, encryptHistory on logoutlaravel-inertia skill
Vite wiring / Wayfinder route generation for the SPAlaravel-frontend skill
Pest auth helpers (actingAs, Sanctum::actingAs)laravel-qa skill
Building the login/2FA UI componentslaravel-react / laravel-vue agents
Session driver infra (Redis), deploy-time session invalidationlaravel-role-devops

Stack assumptions

  • Laravel 12, PHP 8.3+
  • App\Models\User extends Authenticatable
  • Default web (session) and api guards in config/auth.php
  • Detection-based: the agent runs composer show laravel/sanctum laravel/fortify laravel/breeze laravel/passport --quiet 2>/dev/null to discover which auth packages are installed before recommending a flow.

Workflows

Workflow A — Wire Sanctum SPA mode

  1. Detect what's installed: composer show laravel/sanctum laravel/fortify --quiet. Missing? composer require laravel/sanctum && php artisan install:api && php artisan migrate. Confirm the frontend origin (host + port) and whether it's same-origin, cross-subdomain, or cross-site (cross-site → SPA mode does not apply, use tokens).
  2. Configure the 4 touchpoints (all four, every time — see §3.1 for the exact snippets):
    • SANCTUM_STATEFUL_DOMAINS = the frontend origin, host + port, no scheme
    • SESSION_DOMAIN = .example.com (leading dot) for cross-subdomain; bare host for same-origin
    • config/cors.php → supports_credentials: true + explicit allowed_origins (no *)
    • Client → axios withCredentials: true + withXSRFToken: true (fetch: manual X-XSRF-TOKEN header)
    • Plus ->withMiddleware(fn (Middleware $m) => $m->statefulApi()) in bootstrap/app.php.
  3. Wire the client login flow: GET /sanctum/csrf-cookie once per session, then POST /login, then API calls ride the session cookie (§3.1 client flow).
  4. Verify with the end-to-end curl smoke test in references/sanctum_spa_setup.md §11 — CSRF cookie → extract XSRF token → login → authenticated request. Do not call the wiring done until the smoke test passes. On failure, see Troubleshooting and the per-environment matrix in the same reference.
  5. Run the detection greps from the consolidated anti-pattern table — at minimum: throttle on /login, session()->regenerate() in the login path, no tokens in localStorage.

Workflow B — Audit auth in a PR

Run in order; each step maps to a row in the consolidated table below.

  1. Login routes throttled? grep -rn "Route::post('/login'" routes/ — flag any without ->middleware('throttle:...) or ThrottlesLogins.
  2. Session regenerated on login? Review login controllers/actions: Auth::login()/Auth::attempt() must be followed by $request->session()->regenerate() (§2).
  3. Destructive routes confirmed? Review DELETE routes and account/email/token mutations — password.confirm middleware present? (§7)
  4. Auth in queue context? grep -rn "auth()->user()" app/Jobs app/Console — fails outside HTTP; the actor must be passed explicitly and checked via Gate::forUser() (§8.1).
  5. New tokens created with abilities? grep -rn "createToken(" app/ — flag calls without an abilities array, and tokenCan used as the only ownership check (§3.2, §8.2).
  6. Password handling: grep -rn "md5\|sha1" app/ near passwords/tokens; == comparison instead of Hash::check (§9).
  7. Finish with the full consolidated table for anything the diff touches (verified middleware, reset-link throttle, multi-guard smells).

1. The decision tree

NeedUse
Server-rendered Blade app, traditional login formSessions (built-in) — optionally Breeze for scaffolding
Inertia / Vue / React SPA on the same domain or apexSanctum SPA mode (cookie auth)
Mobile app or 3rd-party API clientSanctum API tokens with abilities
Headless flows (no UI scaffolding) — 2FA, password reset, email verificationFortify (often combined with Sanctum SPA)
Full OAuth2 server (you issue tokens to other apps' users)Passport
Both an SPA and a public API on the same appSanctum in both modes (one config)

⚠️ Anti-pattern: Passport when Sanctum suffices. Passport implements full OAuth2 — appropriate when you're an identity provider to third parties, overkill for first-party apps.

⚠️ Anti-pattern: Sanctum tokens for a same-domain SPA when SPA cookie mode is available. Tokens in localStorage are XSS-exposed; cookies are HttpOnly.


2. The session guard (built-in)

Default for any traditional web flow. No extra package.

// Login (manual)
if (Auth::attempt(['email' => $email, 'password' => $password], remember: $remember)) {
    $request->session()->regenerate();           // protect against session fixation
    return redirect()->intended('/dashboard');
}

return back()->withErrors(['email' => 'Invalid credentials.']);

// Logout
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');

Rules:

  • Always session()->regenerate() after login and invalidate() + regenerateToken() after logout. Without these, session fixation and CSRF token reuse are real risks.
  • Use the web guard (default) — no auth('api') for browser flows.
  • Auth::user(), auth()->user(), $request->user() are equivalent inside the request lifecycle.

2.1 Login throttling

Use the trait or the throttle middleware.

// app/Http/Controllers/Auth/LoginController.php
use Illuminate\Foundation\Auth\ThrottlesLogins;

class LoginController extends Controller
{
    use ThrottlesLogins;

    protected int $maxAttempts = 5;
    protected int $decayMinutes = 1;
}

Or per-route: Route::post('/login', ...)->middleware('throttle:5,1');

⚠️ Anti-pattern: custom login endpoint with no throttle. Brute-force trivially exploitable. See laravel-security for credential-stuffing detection patterns.


3. Sanctum

Single package, two distinct modes. Pick one per consumer; the same Laravel app can serve both.

composer require laravel/sanctum
php artisan install:api          # Laravel 11+ scaffold (publishes config + migrations)
php artisan migrate

3.1 SPA cookie mode (the Inertia / first-party SPA default)

The SPA and the API live on the same registrable domain (app.example.com + api.example.com work; cross-site does not). Authentication uses Laravel's session cookie + CSRF — no tokens involved. Wiring steps: Workflow A.

Four config touch points:

// config/sanctum.php
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 'localhost:5173,localhost:8000,127.0.0.1')),
'guard'    => ['web'],
// bootstrap/app.php
->withMiddleware(fn (Middleware $m) => $m->statefulApi())
// config/cors.php
'paths'                => ['api/*', 'sanctum/csrf-cookie', 'login', 'logout'],
'allowed_origins'      => [env('FRONTEND_URL', 'http://localhost:5173')],
'supports_credentials' => true,
# .env (cross-subdomain prod example)
SESSION_DOMAIN=.example.com
SESSION_SAME_SITE=lax
SESSION_SECURE_COOKIE=true
SANCTUM_STATEFUL_DOMAINS=app.example.com

Client flow:

await axios.get('/sanctum/csrf-cookie');     // once per session — sets XSRF-TOKEN + laravel_session
await axios.post('/login', { email, password });
await axios.get('/api/posts');                // authenticated by the session cookie

The four rules that catch most mistakes:

  1. SANCTUM_STATEFUL_DOMAINS is the frontend origin (host + port, no scheme).
  2. SESSION_DOMAIN is .example.com (leading dot) for cross-subdomain; bare host for same-origin.
  3. supports_credentials: true in CORS and withCredentials: true / withXSRFToken: true on the client (axios sets both when configured; fetch requires manual X-XSRF-TOKEN header).
  4. Never mix localhost and 127.0.0.1 — different origins to the browser.

For the full domain-topology decision matrix (apex/subdomain/multi-port), per-environment .env templates (local/staging/prod), SameSite deep-dive, reverse-proxy considerations, the troubleshooting matrix for "419 Page Expired" and "logged out on every request" symptoms, and an end-to-end curl smoke test, see references/sanctum_spa_setup.md.

⚠️ Anti-pattern: mixing auth:sanctum middleware with token logins for an SPA on the same domain. Stick to cookies for SPAs; tokens add XSS surface.

3.2 API token mode

For mobile clients, CLI tools, third-party integrations.

// Issue a token
$token = $user->createToken('mobile-app', ['posts:read', 'posts:write'])->plainTextToken;
return ['token' => $token];

// Protect routes
Route::middleware('auth:sanctum')->get('/api/posts', PostController::class);

// Check abilities
if ($request->user()->tokenCan('posts:write')) { /* ... */ }

// Or as middleware
Route::middleware(['auth:sanctum', 'ability:posts:write'])
    ->post('/api/posts', PostController::class);

Rules:

  • Plain-text token is returned once. Store the hash (personal_access_tokens.token) only. Stand by tokenable_id + name + last_used_at for audit.
  • Abilities are app-defined strings. Define a canonical list in a Service or enum and reuse across createToken, middleware, and policies.
  • Revoke on logout: $request->user()->currentAccessToken()->delete().
  • Rotate aggressively: short-lived tokens + refresh, or per-device tokens revoked from the user's settings page.

For per-token expiration, last-used tracking, transferring abilities to Policies, and Sanctum + Octane gotchas, see laravel-security.


4. Fortify

Headless implementation of the flows Breeze scaffolds with a UI: registration, login, password reset, email verification, two-factor authentication, profile updates, password confirmation. You build the UI. Fortify exposes routes and event hooks.

composer require laravel/fortify
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
php artisan migrate

Config (config/fortify.php) — feature toggles:

'features' => [
    Features::registration(),
    Features::resetPasswords(),
    Features::emailVerification(),
    Features::updateProfileInformation(),
    Features::updatePasswords(),
    Features::twoFactorAuthentication(['confirm' => true, 'confirmPassword' => true]),
],

Customize what each flow does — bind contracts in FortifyServiceProvider:

Fortify::createUsersUsing(CreateNewUser::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::loginView(fn () => Inertia::render('Auth/Login'));

Fortify pairs with Sanctum SPA mode out of the box — login posts to /login (Fortify), session is established, future requests use the cookie.

Rules:

  • Fortify owns the routes; you own the views/components (Inertia Pages, Blade, etc.).
  • Hook business rules via Fortify::createUsersUsing(...) etc. — don't subclass controllers.
  • ⚠️ Anti-pattern: writing your own password reset endpoint when Fortify is installed. Use Fortify; it handles token expiry, single-use, throttle.

4.1 Two-factor authentication

// Enable for current user
$user->forceFill([
    'two_factor_secret'           => encrypt(app(TwoFactorAuthenticationProvider::class)->generateSecretKey()),
    'two_factor_recovery_codes'   => encrypt(json_encode(Collection::times(8, fn () => RecoveryCode::generate()))),
])->save();

// Confirm
POST /user/confirmed-two-factor-authentication { code: '123456' }

Recovery codes are single-use; regenerate after any used. Threat-model details (TOTP drift, brute-force on codes) live in laravel-security.

Full endpoint lifecycle (enable → QR → confirm → recovery codes → login challenge), SPA integration without Blade views, and the 2FA troubleshooting matrix: references/fortify_2fa.md.


5. Breeze

Starter kit: generates auth controllers, Blade/Inertia/Vue/React pages, and routes. Meant to be read, then modified or replaced — not a runtime dependency. Install: composer require laravel/breeze --dev && php artisan breeze:install react (or vue, blade, api), then migrate and build. Use for greenfield apps or as a reference implementation of Sanctum SPA + Fortify-style flows. Avoid on an existing app with custom auth UI — it overwrites views. Beyond that, defer to the official docs.


6. Passport

Full OAuth2 server: issues tokens to third-party clients via standard grant types (authorization code, client credentials). Use only when you are an identity provider to other apps. For first-party SPAs, Sanctum SPA mode is the right answer (§1 anti-pattern). Install: php artisan install:api --passport. Passport's surface area is too large for this skill — defer to the official docs. For the Sanctum-vs-Passport decision matrix, current grant-type status (password grant deprecated), and token ability design, see references/token_mode_decision.md.


7. Middleware

MiddlewarePurpose
authRequires authenticated user (default guard) — redirects to login route on web, returns 401 on JSON
auth:sanctumSanctum-specific — checks SPA cookie OR Authorization: Bearer <token>
auth:web,apiMulti-guard — first guard that authenticates wins
auth.sessionLogout other devices when password changes (requires Authenticate Session table)
verifiedRequires email_verified_at is not null
password.confirmForces re-entry of current password before destructive action
throttle:60,160 requests / 1 minute per user (or IP if guest)
signedSigned URL middleware (used by Fortify for email verification links)
ability:<ability>Sanctum token must have the listed ability
abilities:<a>,<b>Sanctum token must have all listed abilities
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
    Route::get('/dashboard', DashboardController::class);
    Route::middleware('password.confirm')
        ->delete('/account', AccountController::class);
});

⚠️ Anti-pattern: destructive endpoints (delete account, change email, rotate API token) without password.confirm. Lets a hijacked session do permanent damage.


8. Authorization — Policies & Gates

The laravel-backend skill (§13) covers the basics: defining a Policy, calling $this->authorize(), registering Gates, route middleware can:. All of that applies. This skill adds the auth-adjacent angles; deep patterns → references/authorization_patterns.md (local to this skill).

8.1 Authorization in jobs / console

auth()->user() returns null outside an HTTP request. Pass the actor explicitly:

final class PublishPost implements ShouldQueue
{
    public function __construct(public Post $post, public User $actor) {}

    public function handle(): void
    {
        Gate::forUser($this->actor)->authorize('publish', $this->post);
        $this->post->update(['published_at' => now()]);
    }
}

PublishPost::dispatch($post, auth()->user());

Gate::forUser($user)->... is the canonical way to authorize against a non-current user.

8.2 Token abilities vs Policies

  • Abilities (Sanctum) gate what an API client can do — coarse, set at token creation.
  • Policies gate whether this user can act on this resource — fine, evaluated per call.

Combine both: Route::middleware(['auth:sanctum', 'ability:posts:write']) covers the token; the controller still calls $this->authorize('update', $post) for the per-resource check.

⚠️ Anti-pattern: treating token abilities as authorization. They prove the client asked for the capability — they do not prove the user owns the resource.

8.3 Multi-tenant authorization

Combine a global scope (tenant_id) with Policies as defense in depth. Pattern lives in references/authorization_patterns.md (multi-tenant section).

For broader Policy composition, Gate::before/after, super-admin escape hatches, Spatie Permission integration, see the same reference doc.


9. Password hashing & rehash

Laravel uses bcrypt by default (config/hashing.php). Argon2id is available; switch via 'driver' => 'argon2id'.

Hash::make($plain);                            // hash
Hash::check($plain, $user->password);          // verify
Hash::needsRehash($user->password);            // returns true if cost or algorithm changed

Rules:

  • After raising bcrypt rounds or switching algorithms, rehash on next successful login:
    if (Hash::needsRehash($user->password)) {
        $user->update(['password' => Hash::make($plain)]);
    }
    
  • Never compare passwords with ==. Always Hash::check.
  • ⚠️ Anti-pattern: md5($password) or sha1($password) anywhere. Even for "non-sensitive" tokens — a leak teaches users you don't take crypto seriously.

For algorithm trade-offs (bcrypt vs argon2id, cost calibration, timing attacks), see laravel-security — this skill owns the flow code above; that skill owns the threat rationale.


10. Email verification — corrective rules

Standard flow (implement MustVerifyEmail, Fortify/Breeze routes, sendEmailVerificationNotification()) is assumed known. What goes wrong:

  • The verified middleware blocks unverified users. Pair with a "Verify your email" page on the unverified path.
  • Verification links are signed (signed middleware) and have a TTL (config/auth.php#verification.expire, default 60 minutes).
  • ⚠️ Anti-pattern: treating an unverified email as authenticated for important actions. verified middleware belongs on every meaningful route, not just /dashboard.

11. Password reset — corrective rules

Standard flow (Password::sendResetLink() / Password::reset(), hashed token in password_reset_tokens with TTL, Fortify/Breeze views) is assumed known. What goes wrong:

  • Throttle reset-link requests (throttle:6,1 or stricter). Prevents email-bombing a known account.
  • Fire event(new PasswordReset($user)) in the reset callback — listeners (session invalidation, audit) depend on it.
  • Invalidate other sessions after a reset: pair auth.session middleware with the Authenticate Session table.

12. Multi-guard apps

Two distinct user types (e.g. User and Admin) with separate tables and login flows.

// config/auth.php
'guards' => [
    'web'   => ['driver' => 'session', 'provider' => 'users'],
    'admin' => ['driver' => 'session', 'provider' => 'admins'],
],
'providers' => [
    'users'  => ['driver' => 'eloquent', 'model' => App\Models\User::class],
    'admins' => ['driver' => 'eloquent', 'model' => App\Models\Admin::class],
],

// Route protection
Route::middleware('auth:admin')->prefix('admin')->group(...);

// Check
if (Auth::guard('admin')->check()) { /* ... */ }

⚠️ Anti-pattern: modeling "admin" as a flag on the same User model and also adding a separate guard. Pick one. A flag-on-User with Policies is simpler; a separate guard is justified only when admins truly are a different domain entity (different schema, different login UI, different session lifetime).


Rules & anti-patterns (consolidated)

SmellSectionDetection
Passport for first-party SPA§1composer show laravel/passport + Inertia/SPA app
Sanctum tokens for same-domain SPA§1, §3.1tokens stored in localStorage
Custom login endpoint with no throttle§2.1grep Route::post('/login' without ->middleware('throttle')
Auth::login() without session()->regenerate()§2review login controllers
Custom password reset when Fortify is installed§4composer + grep Password::reset outside Fortify
Destructive route without password.confirm§7review DELETE routes
auth()->user() inside a Job/Command§8.1grep auth\(\)->user\(\) in app/Jobs, app/Console
Token abilities used as ownership check§8.2review controllers gating with tokenCan only
New token created without abilities§3.2grep createToken( without an abilities array
md5/sha1 of passwords or auth tokens§9grep md5|sha1 near password
Unverified email allowed past /dashboard§10grep auth middleware without verified
Reset-link endpoint not throttled§11grep sendResetLink route without throttle
Flag-on-User AND separate guard for "admin"§12is_admin column + multiple guards

Troubleshooting

Symptom → likely cause. First stop when Workflow A's smoke test fails.

SymptomLikely cause
"419 Page Expired" right after login (SPA)XSRF cookie not sent — missing withCredentials or wrong CORS / SANCTUM_STATEFUL_DOMAINS
Logged out on every request (SPA)SESSION_DOMAIN doesn't cover both subdomains — set to .example.com
User logged out after deployAPP_KEY rotated or session driver/domain changed between releases — sessions can't decrypt
auth()->user() is null in queued jobNo HTTP context — pass actor explicitly (§8.1)
2FA challenge loopSession lost between login + challenge — check SESSION_DRIVER (don't use array in prod)
Hash::needsRehash always trueDifferent algorithm/cost between hash creation and check — verify config/hashing.php
Token-protected route returns 401 with valid tokenToken revoked, expired, or ability: middleware mismatch — log currentAccessToken()

Deeper SPA-cookie diagnosis (per-environment matrix, SameSite, reverse proxies): references/sanctum_spa_setup.md.


Reference routing

TaskLoad
SPA cookie auth setup, domain topology, per-env .env templates, 419 debugging, CORS matrix, curl smoke testreferences/sanctum_spa_setup.md
Policy composition, Gate::before/after, multi-tenant authorization, Spatie Permission, super-admin escape hatches, authorization in jobs/consolereferences/authorization_patterns.md
Wiring or debugging Fortify 2FA, building the SPA 2FA flow (enable → confirm → challenge), 423/404 challenge symptoms, recovery codesreferences/fortify_2fa.md
Choosing Sanctum tokens vs Passport, designing token abilities, expiration/rotation/revocation, Passport 13 grant statusreferences/token_mode_decision.md

Cross-references

TopicSkill
OWASP, password hashing internals, 2FA threat model, dep CVEslaravel-security
Eloquent User model, FormRequests for login/registerlaravel-backend
Inertia shared auth.user, encryptHistory on logoutlaravel-inertia
Wayfinder route generation, Vite wiringlaravel-frontend
Pest helpers actingAs, Sanctum::actingAs($user, ['ability'])laravel-qa

Keep looking

Skills are one crate of 607,533. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.