简体   繁体   English

Laravel 5.1用于身份验证的额外字段

[英]Laravel 5.1 extra field for authentication

I'm making my first big project using Laravel 5.1 and I'd like to add an extra check during user login to see if the user has activated their account. 我正在使用Laravel 5.1创建我的第一个大项目,我想在用户登录期间添加额外的检查以查看用户是否已激活他们的帐户。

This is the schema of the users table 这是users表的模式

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->boolean('is_admin')->default(true);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

I've tried adding a $credentials['is_active'] = true; 我已经尝试添加$credentials['is_active'] = true; after $credentials = $this->getCredentials($request); $credentials = $this->getCredentials($request); in AutheticatesUser@postLogin and it works but I want to have a custom error if the user's account isn't active because the default one( These credentials do not match our records. ) is not that intuitive for the user. AutheticatesUser@postLogin ,它可以工作,但是如果用户的帐户不活动,我想要有一个自定义错误,因为默认帐户( These credentials do not match our records. )对用户来说并不直观。

Any suggestions in achieving that? 实现这一目标的任何建议? Thank you! 谢谢!

You can override the postLogin method in your AuthController and check whether the user is active or not like this. 您可以覆盖AuthController中的postLogin方法,并检查用户是否处于活动状态。

class AuthController extends Controller
{
public function postLogin(Request $request){
    $this->validate($request, [
          'email' => 'required|email', 'password' => 'required',
    ]);
   $credentials = $this->getCredentials($request);
  // This section is the only change
  if (Auth::validate($credentials)) {
      $user = Auth::getLastAttempted();
      if ($user->is_active) {
          Auth::login($user, $request->has('remember'));
          return redirect()->intended($this->redirectPath());
      } else {
         return redirect($this->loginPath()) // Change this to redirect elsewhee
        ->withInput($request->only('email', 'remember'))
        ->withErrors([
            'active' => 'Please active your account'
          ]);
      }
  }
   return redirect($this->loginPath())
      ->withInput($request->only('email', 'remember'))
      ->withErrors([
          'email' => $this->getFailedLoginMessage(),
   ]);
  }
}

You can check following way 你可以检查以下方式

 if(Auth::attempt(['email'=>$email,'password'=>$password,'is_admin'=>1]))
       {
            return redirect()->intended('admin/dashboard');
       }

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

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