Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uniksk's avatar

Enable/ Disable user email verification with a simple toggle

Hi There,

Scenario

I wish to make a checkbox in the admin panel, which

  • if checked : Verification email should be sent after successful user registration ; 'verified' middleware should check user verification
  • If not checked : Verification email should not be sent; 'verified' middleware should be ignored.

What have I done?

I have tried doing

Auth::routes([
    'verify' => true or false
 ]);

Problem

This only disables the route required for email verification. Since "MustVerifyEmail" has been implemented to the "User" class, It still works. Yeah, I could remove "MustVerifyEmail" from there, but I am trying to toggle it, not disable it.

What I need?

I need a way to toggle the whole user email verify scenario i.e, routes, middlewares, sending email, all of it.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

You can create a boolean field in your users table, such as email_verified, and set it to false by default. Then, you can create a checkbox in your admin panel that will update the email_verified field to true when checked.

You can then use the email_verified field to determine whether or not to send the verification email and check the verified middleware.

For example, in your User model, you can add the following method:

public function shouldSendVerificationEmail()
{
    return $this->email_verified;
}

Then, in your RegisterController, you can use the shouldSendVerificationEmail method to determine whether or not to send the verification email:

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    if ($user->shouldSendVerificationEmail()) {
        $user->sendEmailVerificationNotification();
    }

    return $user;
}

Finally, you can use the email_verified field to determine whether or not to check the verified middleware. For example, in your routes/web.php file, you can add the following:

Route::group(['middleware' => ['auth', 'email_verified']], function () {
    // Your routes here
});

Then, you can create a custom middleware to check the email_verified field:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class EmailVerified
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->email_verified) {
            return $next($request);
        }

        return redirect('/');
    }
}

Then, you can register the middleware in your Kernel.php file:

protected $routeMiddleware = [
    // Other middleware
    'email_verified' => \App\Http\Middleware\EmailVerified::class,
];

Now, when the email_verified field is set to true, the verified middleware will be checked. Otherwise, it will be ignored.

1 like

Please or to participate in this conversation.