英文:
disable login from inactive user using Laravel Breeze
问题
-
你可以编辑
app\Http\Requests\Auth\LoginRequest.php
文件中的authenticate()
方法,以实现禁用不活跃用户的登录和登出已登录用户。具体的编辑方式取决于你的需求,但通常你需要在该方法中添加一些条件,以检查用户的活跃状态并相应地执行登录或登出操作。 -
是的,你可以在登录失败时向不活跃用户显示消息 "只有活跃用户允许登录",而不会使它过于复杂。在
authenticate()
方法中,当登录失败时,你可以修改ValidationException::withMessages()
的内容,将错误消息更改为你希望的消息,如 "只有活跃用户允许登录"。这将在登录失败时向用户显示相应的消息。
英文:
I want to disable inactive user from login, and logout the user if already logged in,
using breeze pack:
breeze pack
i"m having a boolean column: "active"
in users table.
i understand that i should edit
app\Http\Requests\Auth\LoginRequest.php
i understand that i should edit the current code:
public function authenticate(): void
{
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
1.What and how should i edit that (or other) code ?
2.Is it possible to show a message "only active users allowed to login" to inactive user, without make it complecated ?
答案1
得分: 2
以下是代码的翻译部分:
public function authenticate(): void
{
$this->ensureIsNotRateLimited();
$credentials = $this->only('email', 'password');
$user = User::where('email', $credentials['email'])->first();
if (!$user) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
if (!$user->active) {
throw ValidationException::withMessages([
'email' => '只有活跃用户可以登录',
]);
}
if (! Auth::attempt($credentials, $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
希望这对您有所帮助。如果您需要进一步的帮助,请随时告诉我。
英文:
To disable inactive users from logging in, you need to add a condition in the code to check the active status of the user, and to show a message to inactive users, you can add another condition and throw a custom exception with a message.
public function authenticate(): void
{
$this->ensureIsNotRateLimited();
$credentials = $this->only('email', 'password');
$user = User::where('email', $credentials['email'])->first();
if (!$user) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
if (!$user->active) {
throw ValidationException::withMessages([
'email' => 'Only active users are allowed to login',
]);
}
if (! Auth::attempt($credentials, $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
答案2
得分: 1
对于这一点,我会转向注册全局中间件,可以在每个路由上进行检查。您可以通过php artisan make:middleware YourMiddlewareName
命令生成中间件。
在生成的中间件文件中:
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
if ($user->active === 0) {
//Log them out
//Redirect them somewhere with a message
}
}
return $next($request);
}
在这段代码中,我们首先检查是否有用户登录,然后检查已登录的用户是否处于活动状态。从那里,您可以执行所需的步骤。
在登录请求中执行此操作的问题在于用户直到再次登录前不会受到影响。注册全局中间件将使此操作在他们发出的每个请求上触发。
英文:
For this I would turn to registering global middleware that can be checked on each route. You can generate middleware via the php artisan make:middleware YourMiddlewareName
command.
Within the generated middleware file:
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
if ($user->active === 0) {
//Log them out
//Redirect them somewhere with a message
}
}
return $next($request);
}
In this code we can see that we first check that there is a user logged in, then checking if that logged in user is active. From there you can do whatever steps you need to take.
The problem with performing this action within the login request is that the user would not be affected until they log in again. Registering global middleware will make this fire on every request they make.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论