简体   繁体   English

Laravel 5.1:如何仅对活动用户进行身份验证

[英]Laravel 5.1: How to authenticate only active users

I want to add another condition in AuthController but I don't know how to do it. 我想在AuthController添加另一个条件,但是我不知道该怎么做。

In my Users table, I have a Active field. 在我的Users表中,我有一个Active字段。 If a User has Active == 0 , i want to not let he/she login into the system. 如果User Active == 0 ,我不想让他/她登录系统。 I don't know where to add that condition in Laravel 5.1 AuthController . 我不知道在Laravel 5.1 AuthController在哪里添加该条件。

Please help me with that. 请帮我。 Thanks a lot guys! 非常感谢你们!

You can override the postLogin method of the AuthenticatesUsers trait in your AuthController : 您可以覆盖postLogin的方法AuthenticatesUsers在您的特质AuthController

public function postLogin(Request $request)
{ 
    /* PLACE HERE VALIDATION CODE... */  

    //attempt login but not log in the user
    if ( ! Auth::attempt($credentials, false, false) )
    {
        //wrong credentials: redirect to login
    }   

    //CREDENTIALS OK

    //get user model from last attempt
    $user = Auth::getLastAttempted();

    //if user is not active... 
    if (! $user->active)
    {
        //do whathever you want: for example redirect to login    
    }

    //USER IS ACTIVE

    //login the user
    Auth::login($user, $request->has('remember')); 

    //redirect where you need
}

Check the original: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesUsers::postLogin method, to see if you need other instructions inside your overrided method (for example input validation) 检查原始文件: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesUsers::postLogin方法,以查看是否需要其他方法覆盖被覆盖的方法(例如输入验证)

You can add your custom postLogin() function in your Auth controller which overrides the default postLogin function with this condition 您可以在Auth控制器中添加自定义postLogin()函数,该函数将在这种情况下覆盖默认的postLogin函数

Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1))

This way you will only do auth of active users only 这样,您将只对活动用户进行身份验证

Cheers!! 干杯!!

Instead of overriding the whole postLogin function, add this to your AuthController 与其覆盖整个postLogin函数,不如将其添加到您的AuthController中

protected function getCredentials(Request $request)
{
    $crendentials=$request->only($this->loginUsername(), 'password');
    $crendentials['active']=1;
    return $crendentials;
}

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

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