簡體   English   中英

Laravel 5.1:如何僅對活動用戶進行身份驗證

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM