Skip to main content

Installation

Requirements​

  • PHP 8.4 or later
  • Laravel 13
  • Laravel Fortify 1.34.1 or later, with its registration and resetPasswords features enabled

Install the package​

composer require pushery/fortify-hardening-for-laravel

The service provider is registered through package discovery. From the next request on, Fortify's registration, reset-link and reset routes give the neutral answers described in Registration and Password reset. Each of the three can be switched off on its own in the configuration.

Send new accounts a verification mail​

The neutral registration tells every caller to check their inbox, and it signs nobody in. For that to be true for a new account as well, the new account has to receive a mail: implement MustVerifyEmail on your user model, and Laravel sends the verification link when the account is registered.

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
// ...
}

Without it a new account receives nothing, while the holder of a taken address receives the notice. The answer on the screen is still the same, but the person who registered is left waiting for a mail that never comes.

Show the answer on the sign-in page​

A registration from a web form is redirected to the route named login, with the answer flashed as status. Fortify does the same after a password reset: by default its own response redirects to the login route with the message flashed as status, so a sign-in page built for Fortify usually shows it already:

@if (session('status'))
<p role="status">{{ session('status') }}</p>
@endif

A JSON request receives the answer in its body instead, with 202 Accepted.

Prune expired keys on a database store​

A database or a file cache store deletes an expired entry only when its key is read again. The package writes keys that nobody reads again: the note of a completed password reset, the count of an inbox that registers once, and the count of failed sign-ins of an address and IP that never try again. On these stores they stay for good. Schedule the pruning the package ships once a day, in routes/console.php:

use Illuminate\Support\Facades\Schedule;
use Pushery\FortifyHardening\Support\PruneExpiredCacheKeys;

Schedule::call(PruneExpiredCacheKeys::class)->daily();

It prunes the default store and the store in cache.limiter, each once. In a database store it deletes only the expired rows under fortify-hardening:, a thousand to a statement, so a key that has not expired stays, and so does every key of your application. A file store names its files by a hash of the key, so there it deletes every expired file, which the store already treats as missing. On Redis, Memcached and every other store that expires entries itself it does nothing.

Laravel Octane's octane store expires nothing itself and is out of the pruner's reach. It keeps an expired entry in a table of a fixed number of rows, octane.cache.rows, until its key is written again or the server restarts, and the scheduler, where the pruner runs, sees an empty store. Once the table is full, writing a new key fails, so do not make octane the default cache store of an application that runs this package.

Publish the configuration​

Every setting has a default, so publishing is optional. The enabled switches, the mail budget and the minimum duration also read an environment variable, listed under Configuration. To change any other setting, publish the configuration file:

php artisan vendor:publish --tag=fortify-hardening-config
TagPublishes
fortify-hardeningthe configuration file and the translations, in one command
fortify-hardening-configconfig/fortify-hardening.php
fortify-hardening-langthe translations, to lang/vendor/fortify-hardening