更改Laravel 7中“必须验​​证电子邮件”路径的重定向?

时间:2020-08-25 10:36:03

标签: laravel laravel-7

我在Laravel 7中实现了必须验证电子邮件系统。如果用户点击了应用于已验证访问者的路由,则该用户当前将被重定向以查看auth.verify。如何更改此设置并将其重定向到路由user.profile

1 个答案:

答案 0 :(得分:1)

您可以创建一个中间件EnsureEmailIsVerified来覆盖auth.verify

然后将此中间件应用于您的所有路由

use Closure;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Redirect;

class EnsureEmailIsVerified
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $redirectToRoute
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    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('user.profile');
        }

        return $next($request);
    }
}

,然后根据您的要求重定向