覆盖默认 Laravel EnsureEmailIsVerified 中间件未触发
Override default Laravel EnsureEmailIsVerified middleware not firing
我正在使用 Laravel 8 构建休息 api。我想覆盖默认的 EnsureEmailisVerified
中间件并将我拥有的代码从控制器移到该中间件中。
我在登录控制器中用于检查电子邮件是否已验证的代码是;
if (!$user->hasVerifiedEmail()) {
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
效果很好,但我想将其移出控制器,因此我创建了一个中间件并将代码移入其中,如下所示;
class EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next): mixed
{
if (! $request->user() ||
($request->user() instanceof MustVerifyEmail &&
! $request->user()->hasVerifiedEmail())) {
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
return $next($request);
}
}
然后在我的 Kernel.php
文件中添加;
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
然后我的登录路径是这样的;
Route::post('login', [Auth\AuthenticationController::class, 'login'])->middleware('verified');
问题是它根本没有触发...当我点击登录路径时,它似乎没有检查电子邮件是否已验证,只是让用户登录。
您已在身份验证路由 运行 之前将中间件设置为 运行。然而,身份验证路由是用户登录的地方。在中间件 运行s 点,身份验证尚未发生。您需要将中间件切换到主控制器请求后 运行s 的中间件:
class EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next): mixed
{
$response = $next($request);
// $request->user() may also work
if (auth()->check() &&
(auth()->user() instanceof MustVerifyEmail &&
!auth()->user()->hasVerifiedEmail())) {
auth()->logout();
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
return $response;
}
}
我正在使用 Laravel 8 构建休息 api。我想覆盖默认的 EnsureEmailisVerified
中间件并将我拥有的代码从控制器移到该中间件中。
我在登录控制器中用于检查电子邮件是否已验证的代码是;
if (!$user->hasVerifiedEmail()) {
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
效果很好,但我想将其移出控制器,因此我创建了一个中间件并将代码移入其中,如下所示;
class EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next): mixed
{
if (! $request->user() ||
($request->user() instanceof MustVerifyEmail &&
! $request->user()->hasVerifiedEmail())) {
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
return $next($request);
}
}
然后在我的 Kernel.php
文件中添加;
'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
然后我的登录路径是这样的;
Route::post('login', [Auth\AuthenticationController::class, 'login'])->middleware('verified');
问题是它根本没有触发...当我点击登录路径时,它似乎没有检查电子邮件是否已验证,只是让用户登录。
您已在身份验证路由 运行 之前将中间件设置为 运行。然而,身份验证路由是用户登录的地方。在中间件 运行s 点,身份验证尚未发生。您需要将中间件切换到主控制器请求后 运行s 的中间件:
class EnsureEmailIsVerified
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next): mixed
{
$response = $next($request);
// $request->user() may also work
if (auth()->check() &&
(auth()->user() instanceof MustVerifyEmail &&
!auth()->user()->hasVerifiedEmail())) {
auth()->logout();
return response()->json([
'message' => 'You need to confirm your account. We have sent you an activation link to your email.'
], 422);
}
return $response;
}
}