簡體   English   中英

Laravel 5.1用於身份驗證的額外字段

[英]Laravel 5.1 extra field for authentication

我正在使用Laravel 5.1創建我的第一個大項目,我想在用戶登錄期間添加額外的檢查以查看用戶是否已激活他們的帳戶。

這是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();
        });

我已經嘗試添加$credentials['is_active'] = true; $credentials = $this->getCredentials($request); AutheticatesUser@postLogin ,它可以工作,但是如果用戶的帳戶不活動,我想要有一個自定義錯誤,因為默認帳戶( These credentials do not match our records. )對用戶來說並不直觀。

實現這一目標的任何建議? 謝謝!

您可以覆蓋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(),
   ]);
  }
}

你可以檢查以下方式

 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