[英]Optional Email Verification in Laravel
How can I make the email verification optional in Laravel?如何在 Laravel 中使 email 验证成为可选? I have a routes that can be accessed as guest.
我有一条可以作为访客访问的路线。 However, I want all authenticated user to be email verified whenever they access any part of the website including those pages that can be access as guest.
但是,我希望所有经过身份验证的用户在访问网站的任何部分(包括可以以访客身份访问的页面)时都经过 email 验证。
If I'm going to add middleware('verified')
on all routes, guest needs to login to access those public pages.如果我要在所有路由上添加
middleware('verified')
,访客需要登录才能访问这些公共页面。
Example:例子:
Route /
or /posts/1
can be access as a guest. Route
/
或/posts/1
可以作为访客访问。 However, if the user is logged in, I want them to be email verified first to access it.但是,如果用户已登录,我希望他们首先经过 email 验证才能访问它。
The EnsureEmailIsVerified
( verified
) middleware checks to see if the User is authenticated which is why it won't work for guest users. EnsureEmailIsVerified
( verified
) 中间件检查用户是否已通过身份验证,这就是为什么它对来宾用户不起作用的原因。
You could create a new middleware class called something like EnsureEmailIsVerifiedUnlessGuest
(or whatever you want):您可以创建一个新的中间件 class 称为
EnsureEmailIsVerifiedUnlessGuest
(或任何您想要的):
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);
}
Then add it to the $routeMiddleware
array in your app/Http/Kernel.php
file:然后将其添加到
app/Http/Kernel.php
文件中的$routeMiddleware
数组中:
'verified-or-guest' => \App\Http\Middleware\EnsureEmailIsVerifiedUnlessGuest::class,
Then you can use it with your routes with然后你可以将它与你的路线一起使用
->middleware('verified-or-guest')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.