2013-06-04 79 views
7

我很難與Laravel 4驗證::嘗試方法,遵循正確的文檔,閱讀幾個SO線程,但仍然無法讓它工作。Laravel 4驗證:嘗試不工作

$userData = array('email' => '[email protected]','password' => 'admin'); 
if(Auth::attempt($userData)){ 
    // redirect 
} 
else{ 
    echo 'Invalid'; 
} 

,並返回無效的每次

現在,我不知道什麼是真正的原因。

在我的配置/ auth.php我有以下

<?php 

    return array(
/* 
|-------------------------------------------------------------------------- 
| Default Authentication Driver 
|-------------------------------------------------------------------------- 
| 
| This option controls the authentication driver that will be utilized. 
| This drivers manages the retrieval and authentication of the users 
| attempting to get access to protected areas of your application. 
| 
| Supported: "database", "eloquent" 
| 
*/ 

'driver' => 'eloquent', 

/* 
|-------------------------------------------------------------------------- 
| Authentication Model 
|-------------------------------------------------------------------------- 
| 
| When using the "Eloquent" authentication driver, we need to know which 
| Eloquent model should be used to retrieve your users. Of course, it 
| is often just the "User" model but you may use whatever you like. 
| 
*/ 

'model' => 'User', 

/* 
|-------------------------------------------------------------------------- 
| Authentication Table 
|-------------------------------------------------------------------------- 
| 
| When using the "Database" authentication driver, we need to know which 
| table should be used to retrieve your users. We have chosen a basic 
| default value but you may easily change it to any table you like. 
| 
*/ 

'table' => 'users', 

/* 
|-------------------------------------------------------------------------- 
| Password Reminder Settings 
|-------------------------------------------------------------------------- 
| 
| Here you may set the settings for password reminders, including a view 
| that should be used as your password reminder e-mail. You will also 
| be able to set the name of the table that holds the reset tokens. 
| 
*/ 
'reminder' => array(
    'email' => 'emails.auth.reminder', 'table' => 'password_reminders', 
), 
); 
?> 
+0

我告訴過你的問題的答案,downvote它所有你想要的。你的數組密鑰是錯誤的,yoyur登錄將保持失敗。玩的開心。 – CreativityKills

+0

你不應該擔心倒票,你認爲如果嘗試了幾種組合,電子郵件或用戶名至少在laravel4中是不需要的選項http://laravel.com/docs/security。閱讀此文檔以獲得更好的理解.... –

+1

我想你的意思是鏈接到[Laravel 4的文檔。](http://four.laravel.com/docs/security)我現在遇到類似的問題。一旦我找到答案,我會試着在這裏發帖。 –

回答

8

你能顯示代碼,你的模型?這些是應該在用戶模型中才能使Auth運行的一些事情。

<?php 

    use Illuminate\Auth\UserInterface; 
    use Illuminate\Auth\Reminders\RemindableInterface; 

    class User extends Eloquent implements UserInterface, RemindableInterface{ 

    protected $fillable = array('fname','lname','email','password','create_at','updated_at'); 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = array('password'); 

    /** 
    * Get the unique identifier for the user. 
    * 
    * @return mixed 
    */ 
    public function getAuthIdentifier() 
    { 
     return $this->getKey(); 
    } 

    /** 
    * Get the password for the user. 
    * 
    * @return string 
    */ 
    public function getAuthPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Get the e-mail address where password reminders are sent. 
    * 
    * @return string 
    */ 
    public function getReminderEmail() 
    { 
     return $this->email; 
    } 
} 
-3

由於您將錯誤的數組密鑰傳遞到Auth::attempt(),您的代碼正在竊聽。該方法需要一個帶有密鑰用戶名,密碼和可選記憶的數組。在這種情況下,你的上面的代碼應該是:

Route::post('login', function() 
{ 
    $credentials = [ 
     'username' => '[email protected]', 
     'password' => 'superSecretPassword' 
    ]; 

    dd(Auth::attempt($credentials)); 
}); 
+0

Auth :: atempt需要一個數據數組,所以幾乎不需要生成該數組。無論它是一個硬代碼值數組的帖子。 –

+0

不,不重要,但傳遞給該方法的ARRAY KEYS很重要。您通過電子郵件和密碼作爲密鑰,但它是「用戶名」而不是「電子郵件」。這是正確的答案和解決方案,不知道爲什麼有人會投票呢?! – CreativityKills

+2

@CreativityKills人們正在貶低你,因爲你的回答是錯誤的。根據L4文檔:「請注意,電子郵件不是必需的選項,它僅用於例子,您應該使用與數據庫中的」用戶名「相對應的任何列名稱。」參考:http://laravel.com/docs/security#authenticating-users – brazorf

2

我也遇到了一些麻煩。我注意到的是,在我的用戶模型我有:

protected $softDelete = true; 

在我有deleted_at列的數據庫,但它有一個歸零時間戳拖欠 - 0000-00-00 00:00:00。這導致無法找到記錄,從而導致驗證失敗。

我不得不修復遷移到具有像這樣正確創建deleted_at柱:

public function up() 
{ 
    Schema::create('users', function($t) { 
    $t->increments('id'); 
    $t->string('first_name'); 
    $t->string('last_name'); 
    $t->string('username'); 
    $t->string('email'); 
    $t->string('password'); 
    $t->softDeletes(); 
    $t->timestamps(); 
    }); 
} 

下面是軟的文檔刪除:http://laravel.com/docs/eloquent#soft-deleting

10

@埃裏克 - 埃文斯是正確的。要在laravel 4使用的身份驗證,你必須使你的「用戶」模式使用\照亮\身份驗證\的UserInterface接口,並實現

public function getAuthIdentifier() { 
      return $this->getKey(); 
    } 
public function getAuthPassword() { 
      return $this->password; 
     } 
0

的驗證類需要一個列名爲id作爲主鍵的方法,您的用戶表。

+0

不,它不是一個要求.. –

15

確保您的數據庫中的密碼字段有64個字符的空間varchar(64)

哈希需要64個字符,如果哈希密碼在插入時被截斷(因此無法正確驗證密碼),您將不會從laravel中收到錯誤。

+0

(+1)Laravel沒有拋出一個例外,但這解決了我的問題。 –

+0

只想感謝你。我正在試圖解決這個問題,我的頭靠在牆上,這很簡單。 – Lynx

+0

(+1)謝謝你節省我的時間。我有同樣的問題,並沒有宣佈的密碼字段64的大小。我認爲laravel必須顯示一些錯誤,如果大小小於64.可能是他們將來添加此功能:) – neeraj

0

請注意,你的User表,密碼字段是Hashed,Auth :: attempt($ credentials)的原因; $ credentials->密碼評估散列密碼

0

檢查您的密碼是否被散列。它不應該是正常的文本..