DEV Community

Cover image for Laravel: Easily Customize Email Verification URL
Faruk Nasir
Faruk Nasir

Posted on

12 4

Laravel: Easily Customize Email Verification URL

This is not something you'd want to do if you're building an application the traditional laravel way––that is, a monolithic app. I've had to do this recently while working on a project that has the backend and frontend existing in different code bases.

Normally, when you setup a laravel project for new users to verify their email, a temporarily signed url is generated and sent to them. The url is generated based on the project's set APP_URL in the .env file and, subsequently, in the /config/app.php file.

The problem arises when you are building a SPA front end for a laravel backend api. In that case, you may have your frontend at, say, http://cool-app.com and the backend at http://api.cool-app.com. Because the user will not have a direct access to the api endpoints, it won't be ideal to generate a user-clickable link based on the api's base url. What you'd want is a url that is based on the frontend's base url. So, how can that be achieved? Easy.

Create a custom method for generating the URL and pass it to the createUrlUsing method of the VerifyEmail notification class.

/**
* Set a callback that should be used when creating the email verification URL.
*
* @param  \Closure  $callback
* @return void
*/
public static function createUrlUsing($callback)
{
    static::$createUrlCallback = $callback;
}
Enter fullscreen mode Exit fullscreen mode

The only other step that we have to take is to call this function inside the AuthServiceProvider passing the callback that returns our custom URL. The complete code is as below:

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
    ...

    VerifyEmail::createUrlUsing(function ($notifiable) {
        $frontendUrl = 'http://cool-app.com/auth/email/verify';

        $verifyUrl = URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );

        return $frontendUrl . '?verify_url=' . urlencode($verifyUrl);
    });
}
Enter fullscreen mode Exit fullscreen mode

In the frontend, you can have a page with a logic that makes a GET http request to the verify_url. And, there you have it––a customized email verification url!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay