|
| 1 | +--- |
| 2 | +name: developing-with-fortify |
| 3 | +description: Laravel Fortify headless authentication backend development. Activate when implementing authentication features including login, registration, password reset, email verification, two-factor authentication (2FA/TOTP), profile updates, headless auth, authentication scaffolding, or auth guards in Laravel applications. |
| 4 | +--- |
| 5 | + |
| 6 | +# Laravel Fortify Development |
| 7 | + |
| 8 | +Fortify is a headless authentication backend that provides authentication routes and controllers for Laravel applications. |
| 9 | + |
| 10 | +## Documentation |
| 11 | + |
| 12 | +Use `search-docs` for detailed Laravel Fortify patterns and documentation. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +- **Routes**: Use `list-routes` with `only_vendor: true` and `action: "Fortify"` to see all registered endpoints |
| 17 | +- **Actions**: Check `app/Actions/Fortify/` for customizable business logic (user creation, password validation, etc.) |
| 18 | +- **Config**: See `config/fortify.php` for all options including features, guards, rate limiters, and username field |
| 19 | +- **Contracts**: Look in `Laravel\Fortify\Contracts\` for overridable response classes (`LoginResponse`, `LogoutResponse`, etc.) |
| 20 | +- **Views**: All view callbacks are set in `FortifyServiceProvider::boot()` using `Fortify::loginView()`, `Fortify::registerView()`, etc. |
| 21 | + |
| 22 | +## Available Features |
| 23 | + |
| 24 | +Enable in `config/fortify.php` features array: |
| 25 | + |
| 26 | +- `Features::registration()` - User registration |
| 27 | +- `Features::resetPasswords()` - Password reset via email |
| 28 | +- `Features::emailVerification()` - Requires User to implement `MustVerifyEmail` |
| 29 | +- `Features::updateProfileInformation()` - Profile updates |
| 30 | +- `Features::updatePasswords()` - Password changes |
| 31 | +- `Features::twoFactorAuthentication()` - 2FA with QR codes and recovery codes |
| 32 | + |
| 33 | +> Use `search-docs` for feature configuration options and customization patterns. |
| 34 | +
|
| 35 | +## Setup Workflows |
| 36 | + |
| 37 | +### Two-Factor Authentication Setup |
| 38 | + |
| 39 | +``` |
| 40 | +- [ ] Add TwoFactorAuthenticatable trait to User model |
| 41 | +- [ ] Enable feature in config/fortify.php |
| 42 | +- [ ] Run migrations for 2FA columns |
| 43 | +- [ ] Set up view callbacks in FortifyServiceProvider |
| 44 | +- [ ] Create 2FA management UI |
| 45 | +- [ ] Test QR code and recovery codes |
| 46 | +``` |
| 47 | + |
| 48 | +> Use `search-docs` for TOTP implementation and recovery code handling patterns. |
| 49 | +
|
| 50 | +### Email Verification Setup |
| 51 | + |
| 52 | +``` |
| 53 | +- [ ] Enable emailVerification feature in config |
| 54 | +- [ ] Implement MustVerifyEmail interface on User model |
| 55 | +- [ ] Set up verifyEmailView callback |
| 56 | +- [ ] Add verified middleware to protected routes |
| 57 | +- [ ] Test verification email flow |
| 58 | +``` |
| 59 | + |
| 60 | +> Use `search-docs` for MustVerifyEmail implementation patterns. |
| 61 | +
|
| 62 | +### Password Reset Setup |
| 63 | + |
| 64 | +``` |
| 65 | +- [ ] Enable resetPasswords feature in config |
| 66 | +- [ ] Set up requestPasswordResetLinkView callback |
| 67 | +- [ ] Set up resetPasswordView callback |
| 68 | +- [ ] Define password.reset named route (if views disabled) |
| 69 | +- [ ] Test reset email and link flow |
| 70 | +``` |
| 71 | + |
| 72 | +> Use `search-docs` for custom password reset flow patterns. |
| 73 | +
|
| 74 | +### SPA Authentication Setup |
| 75 | + |
| 76 | +``` |
| 77 | +- [ ] Set 'views' => false in config/fortify.php |
| 78 | +- [ ] Install and configure Laravel Sanctum |
| 79 | +- [ ] Use 'web' guard in fortify config |
| 80 | +- [ ] Set up CSRF token handling |
| 81 | +- [ ] Test XHR authentication flows |
| 82 | +``` |
| 83 | + |
| 84 | +> Use `search-docs` for integration and SPA authentication patterns. |
| 85 | +
|
| 86 | +## Best Practices |
| 87 | + |
| 88 | +### Custom Authentication Logic |
| 89 | + |
| 90 | +Override authentication behavior using `Fortify::authenticateUsing()` for custom user retrieval or `Fortify::authenticateThrough()` to customize the authentication pipeline. Override response contracts in `AppServiceProvider` for custom redirects. |
| 91 | + |
| 92 | +### Registration Customization |
| 93 | + |
| 94 | +Modify `app/Actions/Fortify/CreateNewUser.php` to customize user creation logic, validation rules, and additional fields. |
| 95 | + |
| 96 | +### Rate Limiting |
| 97 | + |
| 98 | +Configure via `fortify.limiters.login` in config. Default configuration throttles by username + IP combination. |
| 99 | + |
| 100 | +## Key Endpoints |
| 101 | + |
| 102 | +| Feature | Method | Endpoint | |
| 103 | +|------------------------|----------|---------------------------------------------| |
| 104 | +| Login | POST | `/login` | |
| 105 | +| Logout | POST | `/logout` | |
| 106 | +| Register | POST | `/register` | |
| 107 | +| Password Reset Request | POST | `/forgot-password` | |
| 108 | +| Password Reset | POST | `/reset-password` | |
| 109 | +| Email Verify Notice | GET | `/email/verify` | |
| 110 | +| Resend Verification | POST | `/email/verification-notification` | |
| 111 | +| Password Confirm | POST | `/user/confirm-password` | |
| 112 | +| Enable 2FA | POST | `/user/two-factor-authentication` | |
| 113 | +| Confirm 2FA | POST | `/user/confirmed-two-factor-authentication` | |
| 114 | +| 2FA Challenge | POST | `/two-factor-challenge` | |
| 115 | +| Get QR Code | GET | `/user/two-factor-qr-code` | |
| 116 | +| Recovery Codes | GET/POST | `/user/two-factor-recovery-codes` | |
0 commit comments