Use when implementing user authentication, API tokens, social login, or authorization. Covers Sanctum, Passport, Socialite, Fortify, policies, and gates for Laravel 12.
Resources
1Install
npx skillscat add fusengine/agents/laravel-auth Install via the SkillsCat registry.
SKILL.md
Laravel Authentication & Authorization
Agent Workflow (MANDATORY)
Before ANY implementation, use TeamCreate to spawn 3 agents:
- fuse-ai-pilot:explore-codebase - Check existing auth setup, guards, policies
- fuse-ai-pilot:research-expert - Verify latest Laravel 12 auth docs via Context7
- 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:
| Package | Best For | Complexity |
|---|---|---|
| Starter Kits | New projects, quick setup | Low |
| Sanctum | API tokens, SPA auth | Low |
| Fortify | Custom UI, headless backend | Medium |
| Passport | OAuth2 server, third-party access | High |
| Socialite | Social login (Google, GitHub) | Low |
Critical Rules
- Use policies for model authorization - Not inline
ifchecks - Always hash passwords -
Hash::make()or'hashed'cast - Regenerate session after login - Prevents fixation attacks
- Use HTTPS in production - Required for secure cookies
- 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 featuresFuseCore 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:sanctummiddleware 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-basedToken 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 tokensKey Concepts
| Concept | Description | Reference |
|---|---|---|
| Guards | Define HOW users authenticate (session, token) | authentication.md |
| Providers | Define WHERE users are retrieved from (database) | authentication.md |
| Gates | Closure-based authorization for simple checks | authorization.md |
| Policies | Class-based authorization tied to models | authorization.md |
| Abilities | Token permissions (Sanctum/Passport scopes) | sanctum.md |
Reference Guide
Concepts (WHY & Architecture)
| Topic | Reference | When to Consult |
|---|---|---|
| Authentication | authentication.md | Guards, providers, login flow |
| Authorization | authorization.md | Gates vs policies, access control |
| Sanctum | sanctum.md | API tokens, SPA authentication |
| Passport | passport.md | OAuth2 server, third-party access |
| Fortify | fortify.md | Headless auth, 2FA |
| Socialite | socialite.md | Social login providers |
| Starter Kits | starter-kits.md | Auth scaffolding |
| Email Verification | verification.md | MustVerifyEmail, verified middleware |
| Password Reset | passwords.md | Forgot password flow |
| Session | session.md | Session drivers, flash data |
| CSRF | csrf.md | Form protection, AJAX tokens |
| Encryption | encryption.md | Data encryption (not passwords) |
| Hashing | hashing.md | Password hashing |
Templates (Complete Code)
| Template | When to Use |
|---|---|
| LoginController.php.md | Manual authentication controllers |
| GatesAndPolicies.php.md | Gates and policy examples |
| PostPolicy.php.md | Complete policy class with before filter |
| sanctum-setup.md | Sanctum configuration + testing |
| PassportSetup.php.md | OAuth2 server setup |
| FortifySetup.php.md | Fortify configuration + 2FA |
| SocialiteController.php.md | Social login + testing |
| PasswordResetController.php.md | Password reset flow |
Best Practices
DO
- Use starter kits for new projects
- Define policies for all models
- Set token expiration
- Rate limit login attempts
- Use
verifiedmiddleware 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