mcpField
L
laravel-auth

laravel-auth

github.com/fusengineSecurity3467 installsUpdated 2w ago
Repository

About laravel-auth

A Laravel authentication and authorization skill for developers working with Laravel 13. It covers the framework’s auth ecosystem, security practices, and integration patterns for FuseCore modular projects.

Key features

  1. 01Covers Sanctum, Passport, Fortify, and Socialite
  2. 02Guides auth method and token selection
  3. 03Explains policies, gates, and permissions
  4. 04Covers sessions, CSRF, and password security
  5. 05Provides FuseCore integration patterns

Use cases

  1. 01Choose auth for an SPA or API
  2. 02Configure OAuth2 and third-party access
  3. 03Implement model authorization policies
  4. 04Integrate authentication in FuseCore modules

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/fusengine/claude-plugins --skill laravel-auth

After installing, type/laravel-authin Claude Code to confirm it loaded.

Details

From the project README
Covers the Laravel 13 authentication and authorization ecosystem: Sanctum (API tokens, SPA auth), Passport (OAuth2 server), Fortify (headless custom-UI auth), Socialite (social login), starter kits, policies and gates, email verification, password reset, session management, CSRF / PreventRequestForgery, encryption, and hashing. Includes FuseCore modular- project integration patterns for auth (User module, cross-module authorization via policies).

Laravel Authentication & Authorization

Agent Workflow (MANDATORY)

Before ANY implementation, spawn 3 agents in parallel, one Agent call each with a name:

  1. fuse-ai-pilot:explore-codebase - Check existing auth setup, guards, policies
  2. fuse-ai-pilot:research-expert - Verify latest Laravel 13 auth docs via Context7
  3. mcp__context7__query-docs - Query specific patterns (Sanctum, Passport, etc.)

After implementation, run fuse-ai-pilot:sniper for validation.


Overview

Laravel provides a complete authentication and authorization ecosystem. Choose based on your needs:

PackageBest ForComplexity
Starter KitsNew projects, quick setupLow
SanctumAPI tokens, SPA authLow
FortifyCustom UI, headless backendMedium
PassportOAuth2 server, third-party accessHigh
SocialiteSocial login (Google, GitHub)Low

Critical Rules

  1. Use policies for model authorization - Not inline if checks
  2. Always hash passwords - Hash::make() or 'hashed' cast
  3. Regenerate session after login - Prevents fixation attacks
  4. Use HTTPS in production - Required for secure cookies
  5. Define token abilities - Principle of least privilege

Architecture

app/
├── Http/
│   ├── Controllers/
│   │   └── Auth/              ← Auth controllers (if manual)
│   └── Middleware/
│       └── Authenticate.php   ← Redirects unauthenticated
├── Models/
│   └── User.php               ← HasApiTokens trait (Sanctum)
├── Policies/                  ← Authorization policies
│   └── PostPolicy.php
├── Providers/
│   └── AppServiceProvider.php ← Gate definitions
└── Actions/
    └── Fortify/               ← Fortify actions (if used)
        ├── CreateNewUser.php
        └── ResetUserPassword.php

config/
├── auth.php                   ← Guards & providers
├── sanctum.php                ← API token config
└── fortify.php                ← Fortify features

FuseCore Integration

When working in a FuseCore project, authentication follows the modular structure:

FuseCore/
├── Core/                      # Infrastructure (priority 0)
│   └── App/Contracts/
│       └── AuthServiceInterface.php  ← Auth contract
│
├── User/                      # Auth module (existing)
│   ├── App/
│   │   ├── Models/User.php    ← HasApiTokens trait
│   │   ├── Http/
│   │   │   ├── Controllers/
│   │   │   │   ├── AuthController.php
│   │   │   │   └── TokenController.php
│   │   │   ├── Requests/
│   │   │   │   ├── LoginRequest.php
│   │   │   │   └── RegisterRequest.php
│   │   │   └── Resources/UserResource.php
│   │   ├── Policies/UserPolicy.php
│   │   └── Services/AuthService.php
│   ├── Config/
│   │   └── sanctum.php        ← Sanctum config (module-level)
│   ├── Database/Migrations/
│   ├── Routes/api.php         ← Auth routes
│   └── module.json            # dependencies: []
│
└── {YourModule}/              # Depends on User module
    ├── App/Policies/          ← Module-specific policies
    └── module.json            # dependencies: ["User"]
FuseCore Auth Checklist
  • Auth code in /FuseCore/User/ module
  • Policies in module's /App/Policies/
  • Auth routes in /FuseCore/User/Routes/api.php
  • Sanctum config in /FuseCore/User/Config/sanctum.php
  • Declare "User" dependency in other modules' module.json
  • Use auth:sanctum middleware in module routes
Cross-Module Authorization
// In FuseCore/{Module}/Routes/api.php
Route::middleware(['api', 'auth:sanctum'])->group(function () {
    Route::apiResource('posts', PostController::class);
});

// In FuseCore/{Module}/App/Http/Controllers/PostController.php
public function update(UpdatePostRequest $request, Post $post)
{
    $this->authorize('update', $post);  // Uses PostPolicy
    // ...
}

→ See fusecore skill for complete module patterns.


Decision Guide

Authentication Method
Need auth scaffolding? → Starter Kit
├── Yes → Use React/Vue/Livewire starter kit
└── No → Building custom frontend?
    ├── Yes → Use Fortify (headless)
    └── No → API only?
        ├── Yes → Sanctum (tokens)
        └── No → Session-based
Token Type
Third-party apps need access? → Passport (OAuth2)
├── No → Mobile app?
│   ├── Yes → Sanctum API tokens
│   └── No → SPA on same domain?
│       ├── Yes → Sanctum SPA auth (cookies)
│       └── No → Sanctum API tokens

Key Concepts

ConceptDescriptionReference
GuardsDefine HOW users authenticate (session, token)authentication.md
ProvidersDefine WHERE users are retrieved from (database)authentication.md
GatesClosure-based authorization for simple checksauthorization.md
PoliciesClass-based authorization tied to modelsauthorization.md
AbilitiesToken permissions (Sanctum/Passport scopes)sanctum.md

Reference Guide

Concepts (WHY & Architecture)
TopicReferenceWhen to Consult
Authenticationauthentication.mdGuards, providers, login flow
Authorizationauthorization.mdGates vs policies, access control
Sanctumsanctum.mdAPI tokens, SPA authentication
Passportpassport.mdOAuth2 server, third-party access
Fortifyfortify.mdHeadless auth, 2FA
Socialitesocialite.mdSocial login providers
Starter Kitsstarter-kits.mdAuth scaffolding
Email Verificationverification.mdMustVerifyEmail, verified middleware
Password Resetpasswords.mdForgot password flow
Sessionsession.mdSession drivers, flash data
CSRFcsrf.mdForm protection, AJAX tokens
Encryptionencryption.mdData encryption (not passwords)
Hashinghashing.mdPassword hashing
Templates (Complete Code)
TemplateWhen to Use
LoginController.php.mdManual authentication controllers
GatesAndPolicies.php.mdGates and policy examples
PostPolicy.php.mdComplete policy class with before filter
sanctum-setup.mdSanctum configuration + testing
PassportSetup.php.mdOAuth2 server setup
FortifySetup.php.mdFortify configuration + 2FA
SocialiteController.php.mdSocial login + testing
PasswordResetController.php.mdPassword reset flow

Best Practices

DO
  • Use starter kits for new projects
  • Define policies for all models
  • Set token expiration
  • Rate limit login attempts
  • Use verified middleware for sensitive actions
  • Prune expired tokens regularly
DON'T
  • Store plain text passwords
  • Skip session regeneration on login
  • Use Passport when Sanctum suffices
  • Forget to prune expired tokens
  • Ignore HTTPS in production
  • Put authorization logic in controllers

Laravel 13 Notes

PreventRequestForgery (ex-VerifyCsrfToken)

Laravel 13 renames VerifyCsrfToken to PreventRequestForgery. The middleware adds an origin check via the Sec-Fetch-Site header, falling back to the CSRF token, to block cross-origin CSRF attacks on stateful routes.

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->preventRequestForgery(except: ['stripe/*', 'webhook/*']);
    // $middleware->preventRequestForgery(originOnly: true);    // no token fallback
    // $middleware->preventRequestForgery(allowSameSite: true); // accept same-site
})
  • Migrate every VerifyCsrfToken::class reference → PreventRequestForgery::class (VerifyCsrfToken / ValidateCsrfToken remain as deprecated aliases)
  • Exclusions: preventRequestForgery(except: [...])
  • Stateless APIs (Sanctum tokens) are not affected

Comments

Markdown supported