我在Laravel 7中实现了必须验证电子邮件系统。如果用户点击了应用于已验证访问者的路由,则该用户当前将被重定向以查看auth.verify
。如何更改此设置并将其重定向到路由user.profile
?
答案 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);
}
}
,然后根据您的要求重定向