EnsureEmailIsVerified.php
TLDR
This file, EnsureEmailIsVerified.php
, is a middleware for verifying if a user's email address is verified or not.
Methods
redirectTo($route)
This static method is used to specify the redirect route for the middleware. It takes a route
parameter and returns the class name concatenated with the route name.
handle($request, Closure $next, $redirectToRoute = null)
This method is responsible for handling an incoming request. It checks if the user is not authenticated or if the user is an instance of MustVerifyEmail
and their email is not verified. If either of these conditions is true, the method returns an appropriate response (either a 403 error with the message "Your email address is not verified" if the request expects JSON, or a redirect to the verification notice route if the request does not expect JSON). Otherwise, it passes the request to the next middleware.
Classes
None
<?php
namespace Illuminate\Auth\Middleware;
use Closure;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
class EnsureEmailIsVerified
{
/**
* Specify the redirect route for the middleware.
*
* @param string $route
* @return string
*/
public static function redirectTo($route)
{
return static::class.':'.$route;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $redirectToRoute
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse|null
*/
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::guest(URL::route($redirectToRoute ?: 'verification.notice'));
}
return $next($request);
}
}