简体   繁体   English

只有满足条件的用户才能登录 - Laravel

[英]Login users only if they meet the condition - Laravel

I was wondering where should i put the condition so if a user has status of 1, he can login but if he has not status of 1 he gets redirected back with error message?我想知道我应该把条件放在哪里,这样如果用户的状态为 1,他就可以登录,但是如果他的状态不是 1,他会被重定向回来并显示错误消息?

I will provide you any code you want from controllers etc.我将为您提供您想要的来自控制器等的任何代码。

It's clear if you read section Manually Authenticating Users of the official documentation:如果您阅读官方文档的手动验证用户部分,则很清楚:

If you wish, you may also add extra conditions to the authentication query in addition to the user's e-mail and password.如果您愿意,除了用户的电子邮件和密码之外,您还可以在身份验证查询中添加额外的条件。 For example, we may verify that user is marked as "active":例如,我们可以验证用户是否被标记为“活跃”:

 if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) > { // The user is active, not suspended, and exists. }

I would advise using laravel's built in authentication for a login form.我建议对登录表单使用 laravel 的内置身份验证。 You can read about it here: https://laravel.com/docs/5.1/authentication你可以在这里阅读: https://laravel.com/docs/5.1/authentication

You can do like this你可以这样做

if(auth()->user->status == 0){
   return redirect()->route('login');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM