Overview Modern security standards require more than just a strong password. Laravel now addresses this by providing out-of-the-box support for Two-Factor Authentication (2FA) across its official starter kits. This implementation significantly hardens user accounts by requiring a secondary time-based one-time password (TOTP) from an authenticator app, making it exponentially harder for malicious actors to compromise accounts through credential stuffing or phishing. Prerequisites To follow this guide, you should have a basic understanding of the PHP ecosystem and Composer. Familiarity with terminal commands and local development environments is necessary to scaffold the application. Key Libraries & Tools - **Laravel Fortify**: The backend-agnostic authentication engine that handles the logic for 2FA, password resets, and registration. - **Livewire**: A full-stack framework for Laravel that simplifies building dynamic interfaces without leaving the comfort of Laravel. - **Google Authenticator**: A mobile application used to scan QR codes and generate TOTP codes. - **Pest**: A testing framework frequently used in these starter kits to ensure authentication flows remain stable. Code Walkthrough Setting up 2FA begins during the application scaffolding phase. When you run the installation command, you select your preferred stack: ```bash Creating a new project with Livewire and built-in auth laravel new security-demo --livewire --auth ``` Once the application is running, the user navigates to the **Settings** profile page. Laravel generates a unique secret key and a corresponding QR code. After scanning this with an app like Google Authenticator, the user must provide a valid code to confirm the link. This "confirmation" step is critical; it ensures the user hasn't accidentally locked themselves out by enabling 2FA without a working device. Syntax Notes Configuration for these features lives in the `config/fortify.php` file. You will notice an array of features that can be toggled to customize the security experience: ```python 'features' => [ Features::registration(), Features::resetPasswords(), Features::twoFactorAuthentication([ 'confirm' => true, 'confirmPassword' => true, ]), ] ``` The `confirm` key requires the user to verify a code before the feature is fully active, while `confirmPassword` forces a password challenge before a user can even view their 2FA settings. Tips & Gotchas Always verify that your application's `APP_URL` in the `.env` file is set correctly. The QR code generator uses this URL to label the account inside the authenticator app. If it is left as 'localhost' while in production, your users will have a hard time identifying which code belongs to your service.
Laravel Fortify
Software
- Oct 9, 2025
- Dec 31, 2020