我很难使用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',
),
);
?>
答案 0 :(得分:15)
确保数据库中的密码字段有64个字符的空格。 varchar(64)
哈希需要64个字符,如果你的哈希密码在插入时被截断(因此无法正确验证密码),你就不会从laravel中得到错误。
答案 1 :(得分:10)
@ eric-evans是对的。要在laravel 4中使用身份验证,必须使“用户”模型使用\ Illuminate \ Auth \ UserInterface接口,并实现方法
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
答案 2 :(得分: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 :(得分: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
答案 4 :(得分:0)
Auth类需要一个名为id的列作为users表中的主键。
答案 5 :(得分:0)
注意你的User表,密码字段是Hashed,原因是Auth :: attempt($ credentials); $ credentials-&gt;密码评估哈希密码
答案 6 :(得分:0)
检查您的密码是否经过哈希处理。它不应该是正常的文本..
答案 7 :(得分:-3)
您的代码正在窃听,因为您将错误的数组键传递给Auth::attempt()
。该方法需要一个包含密钥用户名,密码的数组,并可选择记住。鉴于此,您的上述代码应为:
Route::post('login', function()
{
$credentials = [
'username' => 'admin@email.com',
'password' => 'superSecretPassword'
];
dd(Auth::attempt($credentials));
});