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.