Search code examples
laravellaravel-5.7auth-guard

Email Verification in laravel 5.7 for admin guard


I want to implement must verify in admin routes, i have admin guard and all routes related to admin, how can i achieve this functionality for admin guard in Laravel 5.7


Solution

  • This is how I got it to work for me.

    1. Create a custom middleware class and name it what you want, in my case, I named it AdminEmailIsVerified
      class AdminEmailIsVerified extends EnsureEmailIsVerified
      {
        public function handle($request, Closure $next)
        {
           if (! $request->user('admin') ||
              ($request->user('admin') instanceof Admin &&//MustVerifyEmail &&
                  ! $request->user('admin')->hasVerifiedEmail())) {
              return $request->expectsJson()
                  ? abort(403, 'Your email address is not verified.')
                  : Redirect::route('admin.verification.notice');
            }
            return $next($request);
          }
      }

    Kindly note that instance of MustVerifyEmail did not work that is why I went with Admin model.

    1. Register it in your kernel as always,

      'admin.verified' => \App\Http\Middleware\AdminEmailIsVerified::class,

      in the $routesMiddlewareGroup

      I hope this helps

    Admin Dashboard - Email Verification