2015-10-14 71 views
1

我已閱讀Laravel 5 Docs about Authentication,但無法設置如所述的登錄。Laravel5:身份驗證::嘗試不按預期方式工作

我的用戶表中包含兩個字段,用戶名和密碼(這是鍵入文本),我已經存儲在散列密碼這就好比$2y$10$XCyEOyvC6Yp/O6HaeemPheO4KV1I8aEMUytZZt77Yjw9hp/j6uivWnope

這裏是我的代碼:

public function validateLogin() 
{ 
    $email = Input::get('email'); 
    $pass = Input::get('password'); 
    $pass = Hash::make($pass); 

    $credentials = [ 
      'username' => $email, 
      'password' => $pass 
    ]; 
    if (Auth::attempt($credentials)) { 
     // Authentication passed... 
     return "passed"; 
    }else{ 
     return "nope"; 
    } 
} 

我已經嘗試新鮮事物,Auth::attempt始終返回false :( 伊莫它是完全一樣的文檔所描述的,任何人都知道什麼是錯的? 感謝

+0

檢查你的用戶表和配置/ auth.php認證表用戶 –

回答

2

你可能不需要做$pass = Hash::make($password)自己打電話。

嘗試刪除該行,看看它是否工作。

從文檔:

<?php 

namespace App\Http\Controllers; 

use Auth; 
use Illuminate\Routing\Controller; 

class AuthController extends Controller 
{ 
    /** 
    * Handle an authentication attempt. 
    * 
    * @return Response 
    */ 
    public function authenticate() 
    { 
     if (Auth::attempt(['email' => $email, 'password' => $password])) { 
      // Authentication passed... 
      return redirect()->intended('dashboard'); 
     } 
    } 
} 

的嘗試方法接受鍵/值對的陣列作爲它的第一個參數 。數組中的值將用於在您的數據庫表中找到 中的用戶。因此,在上面的示例中,用戶將通過電子郵件列的值檢索 。 如果找到用戶,則將 存儲在數據庫中的散列密碼與通過數組傳遞給方法的散列密碼值 進行比較。

更多參考檢查:

http://laravel.com/docs/master/authentication#authenticating-users

0

沒有ü寫在AuthController功能?或者在任何新的/其他控制器中?

如果u寫在其他控制器的功能,確保你已經添加在上面

這段代碼
use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 




public function login() 
    { 
     $email = Input::get('email'); 
     $password = Input::get('password'); 
     if (Auth::attempt(['email' => $email, 'password' => $password])) { 
      // Authentication passed... 
      return redirect()->intended('dashboard'); 
     } 
     else{ 
      return redirect()->intended('admin/login'); 
     } 
    } 
相關問題