
本文旨在指导开发者在使用 Laravel 8 和 Breeze 搭建的系统中,如何在用户登录时检查其 active 字段(布尔类型)的值,仅允许 active 值为 1 的用户登录。我们将修改 LoginRequest.php 文件中的 authenticate() 方法,加入对用户激活状态的验证,确保只有激活的用户才能成功登录系统。
在使用 Laravel Breeze 搭建的系统中,默认的登录逻辑位于 app/Http/Requests/Auth/LoginRequest.php 文件中。我们需要修改该文件中的 authenticate() 方法,添加对用户 active 字段的验证。
以下是修改后的 authenticate() 方法代码:
/**
* Attempt to authenticate the request's credentials.
*
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
public function authenticate()
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt(array_merge( $this->only('email', 'password'), ['active' => 1 ]), $this->filled('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}代码解释:
注意事项:
总结:
通过修改 LoginRequest.php 文件中的 authenticate() 方法,我们可以轻松地在 Laravel 8 Breeze 系统中实现登录时检查用户激活状态的功能。这可以有效地防止未激活的用户访问系统,提高系统的安全性。请记住根据您的具体需求调整代码,并始终在生产环境中提供清晰的错误消息。
以上就是Laravel 8 Breeze 登录时检查用户是否激活的详细内容,更多请关注php中文网其它相关文章!
Windows激活工具是正版认证的激活工具,永久激活,一键解决windows许可证即将过期。可激活win7系统、win8.1系统、win10系统、win11系统。下载后先看完视频激活教程,再进行操作,100%激活成功。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号