Laravel 5.1 具有状态的身份验证系统

Laravel 5.1 auth system with status

我是新手 laravel 我已经集成了内置的身份验证系统并且运行良好。现在我还想在验证时考虑用户 table 的状态字段。我搜索了很多但找不到将此类功能与内置 laravel 的身份验证系统集成的正确方法。

验证时考虑用户状态很简单。有两种方法可以做到这一点,具体取决于您是手动调用 Auth::attempt() 还是使用 AuthenticatesUsersRegistersAndAuthenticatesUsers 控制器中的特征。

如果您手动调用 Auth::attempt(),您总是将凭据数组传递给该方法。这些凭据用于从数据库中获取用户。将 status 字段添加到这些凭据中,只有具有给定状态的用户才能登录,例如:

Auth::attempt([
    'username' => Request::input('username'),
    'password' => Request::input('password'),
    'status'   => 'active'
]);

以上仅允许 status=active 的用户登录。

如果您在控制器中使用 AuthenticatesUsersRegistersAndAuthenticatesUsers 特性,则需要覆盖 getCredentials() 方法。此方法应该 return 与您传递给 Auth::attempt():

的数组相同
 protected function getCredentials(Request $request) {
  return [
    'username' => Request::input('username'),
    'password' => Request::input('password'),
    'status'   => 'active'
  ];
}