7

我很难使用 Laravel 4 Auth::attempt 方法,遵循正确的文档,阅读了几个 SO 线程,但我仍然无法让它工作。

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

并且每次都返回 Invalid

现在我不确定真正的原因是什么。

在我的 config/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',
),
 );
 ?>
4

8 回答 8

15

确保您的数据库中的密码字段有 64 个字符的空间varchar(64)

哈希需要 64 个字符,如果您的哈希密码在插入时被截断(因此无法积极验证密码),您不会从 laravel 收到错误。

于 2013-12-13T14:52:36.850 回答
10

@eric-evans 是正确的。要在 laravel 4 中使用身份验证,您必须使您的“用户”模型使用 \Illuminate\Auth\UserInterface 接口,并实现方法

public function getAuthIdentifier() {
            return $this->getKey();
  }
public function getAuthPassword() {
            return $this->password;
        }
于 2013-07-28T04:18:10.597 回答
8

你能显示你的模型的代码吗?为了使 Auth 工作,这些是 User 模型中应该包含的一些内容。

<?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;
    }
}
于 2013-06-05T07:24:57.550 回答
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

于 2013-07-10T05:23:28.233 回答
0

Note that you User table , the password field is Hashed, cause of Auth::attempt($credentials); $credentials->password evaluate the hashed password

于 2014-06-21T12:54:30.840 回答
0

Auth 类需要一个名为 id 的列作为用户表中的主键。

于 2013-08-27T20:38:38.823 回答
0

检查您的密码是否经过哈希处理。它不应该是正常的文本..

于 2015-09-18T20:57:06.580 回答
-3

您的代码出错了,因为您将错误的数组键传递给Auth::attempt(). 该方法需要一个包含用户名、密码和可选记住键的数组。鉴于此,您的上述代码应为:

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

    dd(Auth::attempt($credentials));
});
于 2013-06-17T07:04:09.120 回答