fsylum.net

Override Laravel Fortify Status Response

· 2 minutes read

I recently started a new Laravel project using the latest React starter kits. Out of the box, it is using Laravel Fortify to power the authentication functionality. On the older version, the related controllers are published to your app directory so any modifications are pretty straightforward to do.

In the latest version, most of it are abstracted and customisation is only available via FortifyServiceProvider.

For some reason, the status response returned once the app sent the email verification notification is verification-link-sent. It will then be checked in the template and a human-readable version will be displayed to the user instead. This is contrary on how the rest of the templates in starter kits work, so I would like to keep it consistent.

The status message should be set in the backend and not the template responsibility.

In order to override this, few changes are required.

First, create an alternate version of EmailVerificationNotificationSentResponse class. For me, I put this in App/Http/Responses directory with the same name EmailVerificationNotificationSentResponse.php. The only change required here is instead of returning Fortify::VERIFICATION_LINK_SENT, I replaced it with the human-readable status message.

<?php

declare(strict_types=1);

namespace App\Http\Responses;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse as EmailVerificationNotificationSentResponseContract;

class EmailVerificationNotificationSentResponse implements EmailVerificationNotificationSentResponseContract
{
    /**
     * Create an HTTP response that represents the object.
     *
     * @param mixed $request
     */
    public function toResponse($request): JsonResponse|RedirectResponse
    {
        return $request->wantsJson()
            ? new JsonResponse('', 202)
            : back()->with('status', __('A new verification link has been sent to the email address you provided during registration.'));
    }
}

Next is to update the implementation of Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse in the container. In the FortifyServiceProvider, replace the original Fortify’s response class with the custom class created previously.

<?php
// app/Providers/FortifyServiceProvider.php

use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse as EmailVerificationNotificationSentResponseContract;
use App\Http\Responses\EmailVerificationNotificationSentResponse;

class FortifyServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->app->singleton(
            EmailVerificationNotificationSentResponseContract::class,
            EmailVerificationNotificationSentResponse::class
        );
    }
}

With this done, now the status returned once an email verification notification sent will be using the human-readable version and we can output it as-is in the template.