Configuring email verification in Laravel Spark

Scott Anderson
2 min readMay 30, 2020

--

This is a quick guide for setting up Laravel’s email verification functionality for registered users within Laravel Spark.

There is documentation for this however I found it lacking in detail.

I’m using Laravel v7.12.0 and Spark v10.0.

1. Edit your App/User model to implement MustVerifyEmail

In app/User.php change:

class User extends SparkUser

to

class User extends SparkUser implements MustVerifyEmail

Also remember to add the corresponding use Illuminate\Contracts\Auth\MustVerifyEmail;

2. Update SparkServiceProvider.php

Edit app/Providers/SparkServiceProvider.php and in the register() function add:

Spark::ensureEmailIsVerified();

3. Check a few things…

I didn’t need to do any of these, but other developers have reported needing to.

a) Run php artisan migrate and ensure your User table has an ‘email_verified_at’ field
b) Check that $routeMiddleware in app/Http/Kernel.php contains:

’verified’ => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

4. Ensure the ‘verified’ middleware is being checked for any authenticated routes you want to apply email verification to

By default Spark has a HomeController; in the constructor it has

$this->middleware(‘auth’);
//$this->middleware(‘subscribed’);
//$this->middleware(‘verified’);

Note that the ‘verified’ is commented out. Uncomment this to ensure email verification is checked for users accessing routes on this Controller.

An easier way to apply specific middleware to a whole set of routes is to use the ‘group’ function in your routes file.

E.g. In routes/web.php

Use something like

Route::middleware([‘verified’, ‘subscribed’, ‘auth’])->group(function () {
Route::get(‘/home’, ‘HomeController@show’);
});

Now if you have new or existing users, who haven’t verified their email address, they should be shown a “Verify Email Address” page instead of the protected routes.

You’ll also need working email to receive the verification emails. If you need a test SMTP server, you can use https://mailtrap.io/ and add the settings to your .env file.

All done!

More Spark tutorials

Setting up Stripe plans and Spark billing

Setting up Laravel Spark for local development

--

--

Scott Anderson

Full stack engineer @ beamible.com | React / Nextjs, Typescript, GraphQL, Laravel, AWS, GCP | Author of https://buff.ly/2yZYw3Z