2016-12-31 144 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(); 
}