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


Laravel 5.1: How to authenticate only active users

我想在AuthController中添加另一个条件,但我不知道怎么做。

在我的Users表中,我有一个Active字段。如果一个UserActive == 0,我不想让他/她登录系统。我不知道在Laravel 5.1 AuthController中在哪里添加该条件。

请帮我一下。谢谢大家!

您可以在AuthController:中覆盖AuthenticatesUsers特征的postLogin方法

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
}

检查原始的:vendor'laravel'framework'src'Illuminate'Foundation'Auth'AuthenticatesUsers::postLogin方法,看看你是否需要在你的重写方法中使用其他指令(例如输入验证)

您可以在Auth控制器中添加自定义postLogin()函数,该函数将覆盖具有此条件的默认postLogin函数

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

通过这种方式,您将只对活动用户进行身份验证(仅)

干杯!!

与其覆盖整个postLogin函数,不如将其添加到AuthController

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