2016-05-17 116 views
6

我正在使用laravel手動身份驗證系統。提交表單重定向到此路由如下所示。而在authenticate()函數中,名稱和密碼永遠不會與我先前存儲的名稱和密碼匹配。即Auth::attempt總是錯誤的。Laravel手動身份驗證:身份驗證::嘗試始終爲假

Route::post('/logintest', '[email protected]'); 
    Route::get('/home', ['middleware' => 'auth', function() { 
    echo "home page";}); 
}]); 

身份驗證功能:

public function authenticate(Request $request) 
     { 
      $input=$request->all(); 
      $password=$input['password']; 
      $name=$input['name']; 

      if (Auth::attempt(['Name' => $name, 'Password' => $password])){ 
      return redirect()->intended('/home'); 
     } else 
      { 
       return redirect('/login')->with('message','Error logging in!'); 
      } 
     } 

我已經註冊了用戶這種方式。密碼使用bcrypt()散列。功能。但在authenticate()函數中,我正在與純密碼進行比較。我在某處讀Auth會自動處理它。或者有什麼我應該改變在config/auth.php,因爲我用名稱進行身份驗證,而不是用戶名?

public function register(Request $request) 
{ 
    $input=$request->all(); 
    $password=bcrypt($input['password']); 
    $name=$input['name']; 
    $insert= User::insert(['Name'=>$name,'Password'=>$password]); 
    return redirect('/login') 
      ->with('message','successfully Registered.'); 
} 
+0

你能告訴我們路線文件嗎? – VipindasKS

+0

@VipindasKS我已經把它們顯示在頂部。 – micky

+0

這是唯一定義的路線。只是想確保它不與任何其他路線衝突。特別是,如果你早先做過一個php工匠make:auth。 – VipindasKS

回答

0

一切似乎是正確的。

users表中的列名是什麼?

名稱區分大小寫。所以請確保它們確實是NamePassword而不是namepassword

+0

列名稱沒有問題。 – micky

+0

'在公共函數authenticate(Request $ request)'嘗試改變'$ password = $ input ['password'];'to'$ password = bcrypt($ input ['password']);' – linuxartisan

3

檢查以下

public function authenticate(Request $request) 
{ 
    $password = $request->input('password'); 
    $name = $request->input('name'); 

    if (Auth::attempt(['name' => $name, 'password' => $password])) 
    { 
      return redirect()->intended('/home'); 
    } 
    else 
    { 
      return view('login')->withErrors('Error logging in!'); 
    } 
} 
+0

發現'Auth :: attempt'由於密碼錯誤而爲false。我在註冊用戶時使用了bcrypt()。 '$ password = bcrypt($ input ['password']);'我如何在這裏使用它來訪問? – micky

+1

$ password = Input :: get('password'); $ hayhed = Hash :: make($ password); –

+0

試過。沒有解決。 – micky

0

代碼你爲什麼不使用命令php artisan make:auth?它會做你需要的一切。

0
Route::group(['middleware' => ['web']], function() { 
    Route::post('/logintest', '[email protected]'); 
}); 
  1. 請與您routes.php文件上面的更改檢查,只要您使用的版本5或5.2

  2. 確保用戶表字段名稱是「名」和「密碼」否則更新它。

  3. 檢查您的Password字段的字段長度(在您的數據庫中,users表)。它應該保持一個漫長的,哈希密碼這樣的事情$2y$10$eM.kmjTwEIykhNUqMsNzMud0E6eO6RUYAzTqirrbozY1zdhVwQmsC ATLEAST,(VARCHAR(60))

這將是更好,如果你能向我們展示用戶表模式

  • 最後,確保你輸入了正確的密碼(因爲我看不到你的代碼更錯誤)
  • +0

    檢查了所有要點。仍然沒有任何幫助。 – micky

    +0

    你可以請求顯示'用戶'表的表結構嗎? – VipindasKS

    +0

    是的。我將「密碼」更改爲「密碼」,並突然生效。感謝您的努力。 – micky

    3

    沒有與該名稱的問題。 [email protected]將除了password(區分大小寫)之外的所有這些證書都傳遞給該數組並且運行where查詢(這是您可以爲該嘗試添加額外約束的地方,因爲它們只是在哪裏條件下)。如果它找到了一個模型,它會對你通過的password憑證(區分大小寫)和模型的散列密碼進行散列檢查,從$model->getAuthPassword()得到密碼。

    憑證中的這個字段是一個特殊的字段,因爲它是Auth需要的,所以它知道憑證中的字段是密碼。它不直接與您在users表中使用的字段關聯,並且在憑證數組中必須命名爲password。除了「密碼」外,您通過的憑據中的其他字段與用戶表上的字段直接相關,因爲它們是該表上數據庫查詢的條件。

    如果您在表上使用除「密碼」之外的字段作爲密碼,則必須在用戶模型中聲明。在你的情況下,你正在使用'密碼'。 (這是所有區分大小寫)

    class User .... 
    { 
        ... 
        public function getAuthPassword() 
        { 
         return $this->Password; // case sensitive 
        } 
        ... 
    } 
    

    當傳遞憑據傳遞明文密碼一樣會有一個hash_check發生的事情,而不是直接比較。

    你可以在實際的表上命名字段,你只需要讓Eloquent意識到這一點。

    0

    如果你想使用Name作爲唯一的用戶名和密碼fiendname爲Password

    你可以做到這一點的方式..

    在你AuthController添加此方法

    public function loginUsername() 
    { 
        return 'Name'; 
    } 
    

    而用戶型號加入此方法

    public function getAuthPassword() 
    { 
        return $this->Password; 
    } 
    

    希望這會起作用。

    +0

    他們沒有使用默認的AuthController。 – lagbox

    1

    由於您使用名稱而不是電子郵件(默認)作爲用戶名進行身份驗證。您應該在AuthController內添加$username屬性。

    .... 
    
    class AuthController extends Controller 
    { 
    
    
    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 
    
    /** 
    * Override the input name 'email' 
    * Change it as the name on blade 
    * 
    * @var string $username 
    */ 
    protected $username = 'Name'; 
    
    .... 
    } 
    

    或者,你可以從Illuminate\Foundation\Auth\AuthenticatesUsers特質覆蓋loginUsername()方法。

    .... 
    
    class AuthController extends Controller 
    { 
    
    
    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 
    
    /** 
    * Get the login username to be used by the controller. 
    * 
    * @return string 
    */ 
    public function loginUsername() 
    { 
        return 'Name'; 
    } 
    
    .... 
    } 
    

    和其他人一樣,案件事宜。然後,您需要在您的User模型

    .... 
    
    class User extends Authenticatable 
    { 
    
    .... 
    
        /** 
        * Get the password for the user. 
        * 
        * @return string 
        */ 
        public function getAuthPassword() 
        { 
         return $this->Password; 
        } 
    } 
    
    +0

    他們沒有使用AuthController,如果他們是繞過所有這些,因爲他們的代碼中的'attempt'方法是手寫的。 – lagbox

    0

    嘗試從Illuminate\Auth\Authenticatable特質覆蓋getAuthPassword()方法來轉換爲小寫指數在這一行:

    //... 
    if (Auth::attempt(['Name' => $name, 'Password' => $password])) { 
    //... 
    

    //... 
    if (Auth::attempt(['name' => $name, 'password' => $password])) { 
    //... 
    
    0

    你應該寫密碼的小字母中的p字符。

    更換,

    Auth::attempt(['Name' => $name, 'Password' => $password]) 
    

    Auth::attempt(['Name' => $name, 'password' => $password]) // 'p' is in small letter here. 
    

    檢查這個link也。