[英]Laravel 5.1: How to authenticate only active users
我想在AuthController
添加另一個條件,但是我不知道該怎么做。
在我的Users
表中,我有一個Active
字段。 如果User
Active == 0
,我不想讓他/她登錄系統。 我不知道在Laravel 5.1 AuthController
在哪里添加該條件。
請幫我。 非常感謝你們!
您可以覆蓋postLogin
的方法AuthenticatesUsers
在您的特質AuthController
:
public function postLogin(Request $request)
{
/* PLACE HERE VALIDATION CODE... */
//attempt login but not log in the user
if ( ! Auth::attempt($credentials, false, false) )
{
//wrong credentials: redirect to login
}
//CREDENTIALS OK
//get user model from last attempt
$user = Auth::getLastAttempted();
//if user is not active...
if (! $user->active)
{
//do whathever you want: for example redirect to login
}
//USER IS ACTIVE
//login the user
Auth::login($user, $request->has('remember'));
//redirect where you need
}
檢查原始文件: vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\AuthenticatesUsers::postLogin
方法,以查看是否需要其他方法覆蓋被覆蓋的方法(例如輸入驗證)
您可以在Auth控制器中添加自定義postLogin()函數,該函數將在這種情況下覆蓋默認的postLogin函數
Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1))
這樣,您將只對活動用戶進行身份驗證
干杯!!
與其覆蓋整個postLogin函數,不如將其添加到您的AuthController中
protected function getCredentials(Request $request)
{
$crendentials=$request->only($this->loginUsername(), 'password');
$crendentials['active']=1;
return $crendentials;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.