Search code examples
laravelemail-verificationlaravel-10

Customize VerifyEmail url in laravel 10


I’m trying to customize the url embedded inside the verification email, i want to redirect the user to the frontend domain, not on the api domain.


// …

class AuthServiceProvider extends ServiceProvider
{

    // …

    public function boot():void
    {

        VerifyEmail::createUrlUsing(function (/* Get some cb variables here */) {

        //  Update the url here !
        });

    }

}


Solution

  • Here how to change the url based on model. This code added a custom attribute which can be accessed using $request->query('type').

    public function boot(): void
    {
        VerifyEmail::createUrlUsing(function ($notifiable) {
            if ($notifiable instanceof \App\Models\User) {
                return URL::temporarySignedRoute(
                    'verification.verify',
                    Carbon::now()->addMinutes(120),
                    [
                        'id' => $notifiable->getKey(),
                        'hash' => sha1($notifiable->getEmailForVerification()),
                        'type' => 'type1',
                    ]
                );
            } elseif ($notifiable instanceof \App\Models\Frontend\AgentRequests) {
                return URL::temporarySignedRoute(
                    'verification.verify',
                    Carbon::now()->addMinutes(120),
                    [
                        'id' => $notifiable->getKey(),
                        'hash' => sha1($notifiable->getEmailForVerification()),
                        'type' => 'type2',
                    ]
                );
            }
        });
    }
    

    You can modify the URL instead of returning the link. You can use str_replace() function to strip the domain part.