2013-06-04 114 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

检查您的密码是否被散列。它不应该是正常的文本..