2016-12-31 38 views
1

接收參數,這是我的AccountController類:Laravel AUTH嘗試從列表

public function postLogin(Request $request) { 
    if (Auth::attempt(['email' => $request ['email'], 'password' => $request ['password']])) { 
     $auth = \Auth::user()->role_id; 

     switch($auth){ 
      case '1': return redirect()->route('admin/index'); 
      break; 
      case '2': return redirect()->route('client/home'); 
      break; 
      case '3': return redirect()->route('messenger/home'); 
      break; 
     } 
    } 
} 

我希望我的Auth::attempt方法登錄的用戶,如果只有特定用戶的confirmation_codetrue/1

我該怎麼辦?接受Auth :: attempt函數中的特定用戶confirmation_code?

回答

2

只需添加confirmation_code給你傳遞給attempt()法陣:

Auth::attempt([ 
    'email' => $request['email'], 
    'password' => $request['password'], 
    'confirmation_code' => true, 
]); 

Laravel的authentication manual明確規定,您可以添加任意的條件。引用:

如果您願意,除了用戶的電子郵件和密碼之外,還可以爲認證查詢添加額外條件。例如,我們可以驗證用戶標記爲「主動」:

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) { 
    // The user is active, not suspended, and exists. 
} 

如果你很好奇,這裏發生了什麼under the hood;請注意0​​:

/** 
* Retrieve a user by the given credentials. 
* 
* @param array $credentials 
* @return \Illuminate\Contracts\Auth\Authenticatable|null 
*/ 
public function retrieveByCredentials(array $credentials) 
{ 
    if (empty($credentials)) { 
     return; 
    } 

    // First we will add each credential element to the query as a where clause. 
    // Then we can execute the query and, if we found a user, return it in a 
    // Eloquent User "model" that will be utilized by the Guard instances. 
    $query = $this->createModel()->newQuery(); 

    foreach ($credentials as $key => $value) { 
     if (! Str::contains($key, 'password')) { 
      $query->where($key, $value); 
     } 
    } 

    return $query->first(); 
}