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
Asked
Active
Viewed 1,783 times
2 Answers
4
This is how I got it to work for me.
- 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.
Register it in your kernel as always,
'admin.verified' => \App\Http\Middleware\AdminEmailIsVerified::class,
in the $routesMiddlewareGroup
I hope this helps

anabeto93
- 307
- 3
- 12
2
In laravel6, laravel7, we can do it by passing route name in middleware parameter. For example:
Route::middleware('verified:admin.verification.notice')->get('/', 'AdminController@home')->name('home');
Here "dashboard.verification.notice" is the name of verify email route for my admin guard.
===================================================================
Explanation:
Take a look on "handle" method of "EnsureEmailIsVerified" middleware.
public function handle($request, Closure $next, $redirectToRoute = null)
{
if (! $request->user() ||
($request->user() instanceof MustVerifyEmail &&
! $request->user()->hasVerifiedEmail())) {
return $request->expectsJson()
? abort(403, 'Your email address is not verified.')
: Redirect::route($redirectToRoute ?: 'verification.notice');
}
return $next($request);
}
It's 3rd parameter take the $redirectToRoute name

protanvir993
- 2,759
- 1
- 20
- 17
-
The answer by anabeto93 works but this is the correct and the recommended way of doing it thanks – Stanley Feb 28 '20 at 18:51
-
This works fine for Laravel 7. It is better than creating a middleware just to redirect to user to a custom route. – Mycodingproject Oct 22 '20 at 09:38