$npx -y skills add fusengine/agents --skill laravel-authUse when implementing user authentication, API tokens, social login, or authorization. Covers Sanctum, Passport, Socialite, Fortify, policies, and gates for Laravel 13.
| 1 | # Laravel Authentication & Authorization |
| 2 | |
| 3 | ## Agent Workflow (MANDATORY) |
| 4 | |
| 5 | Before ANY implementation, use `TeamCreate` to spawn 3 agents: |
| 6 | |
| 7 | 1. **fuse-ai-pilot:explore-codebase** - Check existing auth setup, guards, policies |
| 8 | 2. **fuse-ai-pilot:research-expert** - Verify latest Laravel 13 auth docs via Context7 |
| 9 | 3. **mcp__context7__query-docs** - Query specific patterns (Sanctum, Passport, etc.) |
| 10 | |
| 11 | After implementation, run **fuse-ai-pilot:sniper** for validation. |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## Overview |
| 16 | |
| 17 | Laravel provides a complete authentication and authorization ecosystem. Choose based on your needs: |
| 18 | |
| 19 | | Package | Best For | Complexity | |
| 20 | |---------|----------|------------| |
| 21 | | **Starter Kits** | New projects, quick setup | Low | |
| 22 | | **Sanctum** | API tokens, SPA auth | Low | |
| 23 | | **Fortify** | Custom UI, headless backend | Medium | |
| 24 | | **Passport** | OAuth2 server, third-party access | High | |
| 25 | | **Socialite** | Social login (Google, GitHub) | Low | |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Critical Rules |
| 30 | |
| 31 | 1. **Use policies for model authorization** - Not inline `if` checks |
| 32 | 2. **Always hash passwords** - `Hash::make()` or `'hashed'` cast |
| 33 | 3. **Regenerate session after login** - Prevents fixation attacks |
| 34 | 4. **Use HTTPS in production** - Required for secure cookies |
| 35 | 5. **Define token abilities** - Principle of least privilege |
| 36 | |
| 37 | --- |
| 38 | |
| 39 | ## Architecture |
| 40 | |
| 41 | ``` |
| 42 | app/ |
| 43 | ├── Http/ |
| 44 | │ ├── Controllers/ |
| 45 | │ │ └── Auth/ ← Auth controllers (if manual) |
| 46 | │ └── Middleware/ |
| 47 | │ └── Authenticate.php ← Redirects unauthenticated |
| 48 | ├── Models/ |
| 49 | │ └── User.php ← HasApiTokens trait (Sanctum) |
| 50 | ├── Policies/ ← Authorization policies |
| 51 | │ └── PostPolicy.php |
| 52 | ├── Providers/ |
| 53 | │ └── AppServiceProvider.php ← Gate definitions |
| 54 | └── Actions/ |
| 55 | └── Fortify/ ← Fortify actions (if used) |
| 56 | ├── CreateNewUser.php |
| 57 | └── ResetUserPassword.php |
| 58 | |
| 59 | config/ |
| 60 | ├── auth.php ← Guards & providers |
| 61 | ├── sanctum.php ← API token config |
| 62 | └── fortify.php ← Fortify features |
| 63 | ``` |
| 64 | |
| 65 | --- |
| 66 | |
| 67 | ## FuseCore Integration |
| 68 | |
| 69 | When working in a **FuseCore project**, authentication follows the modular structure: |
| 70 | |
| 71 | ``` |
| 72 | FuseCore/ |
| 73 | ├── Core/ # Infrastructure (priority 0) |
| 74 | │ └── App/Contracts/ |
| 75 | │ └── AuthServiceInterface.php ← Auth contract |
| 76 | │ |
| 77 | ├── User/ # Auth module (existing) |
| 78 | │ ├── App/ |
| 79 | │ │ ├── Models/User.php ← HasApiTokens trait |
| 80 | │ │ ├── Http/ |
| 81 | │ │ │ ├── Controllers/ |
| 82 | │ │ │ │ ├── AuthController.php |
| 83 | │ │ │ │ └── TokenController.php |
| 84 | │ │ │ ├── Requests/ |
| 85 | │ │ │ │ ├── LoginRequest.php |
| 86 | │ │ │ │ └── RegisterRequest.php |
| 87 | │ │ │ └── Resources/UserResource.php |
| 88 | │ │ ├── Policies/UserPolicy.php |
| 89 | │ │ └── Services/AuthService.php |
| 90 | │ ├── Config/ |
| 91 | │ │ └── sanctum.php ← Sanctum config (module-level) |
| 92 | │ ├── Database/Migrations/ |
| 93 | │ ├── Routes/api.php ← Auth routes |
| 94 | │ └── module.json # dependencies: [] |
| 95 | │ |
| 96 | └── {YourModule}/ # Depends on User module |
| 97 | ├── App/Policies/ ← Module-specific policies |
| 98 | └── module.json # dependencies: ["User"] |
| 99 | ``` |
| 100 | |
| 101 | ### FuseCore Auth Checklist |
| 102 | |
| 103 | - [ ] Auth code in `/FuseCore/User/` module |
| 104 | - [ ] Policies in module's `/App/Policies/` |
| 105 | - [ ] Auth routes in `/FuseCore/User/Routes/api.php` |
| 106 | - [ ] Sanctum config in `/FuseCore/User/Config/sanctum.php` |
| 107 | - [ ] Declare `"User"` dependency in other modules' `module.json` |
| 108 | - [ ] Use `auth:sanctum` middleware in module routes |
| 109 | |
| 110 | ### Cross-Module Authorization |
| 111 | |
| 112 | ```php |
| 113 | // In FuseCore/{Module}/Routes/api.php |
| 114 | Route::middleware(['api', 'auth:sanctum'])->group(function () { |
| 115 | Route::apiResource('posts', PostController::class); |
| 116 | }); |
| 117 | |
| 118 | // In FuseCore/{Module}/App/Http/Controllers/PostController.php |
| 119 | public function update(UpdatePostRequest $request, Post $post) |
| 120 | { |
| 121 | $this->authorize('update', $post); // Uses PostPolicy |
| 122 | // ... |
| 123 | } |
| 124 | ``` |
| 125 | |
| 126 | → See [fusecore skill](../fusecore/SKILL.md) for complete module patterns. |
| 127 | |
| 128 | --- |
| 129 | |
| 130 | ## Dec |