Laravel 4 Auth:尝试不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16916765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Auth:attempt not working
提问by Aman Virk
I am having hard time working with Laravel 4 Auth::attempt method , followed the right documentation, read couple of SO threads but still i am not able to get it working.
我很难使用 Laravel 4 Auth::attempt 方法,遵循正确的文档,阅读了几个 SO 线程,但我仍然无法让它工作。
$userData = array('email' => '[email protected]','password' => 'admin');
if(Auth::attempt($userData)){
// redirect
}
else{
echo 'Invalid';
}
And it returns Invalid everytime
它每次都返回 Invalid
Now i am not sure what is the actual reason.
现在我不确定真正的原因是什么。
In my config/auth.php i have following
在我的 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',
),
);
?>
回答by Isabell Knauer
Make sure that your password field in your db has space for 64 characters. varchar(64)
确保您的数据库中的密码字段有 64 个字符的空间。varchar(64)
The hash needs 64 characters and you won't get an error from laravel if your hashed password is truncated on insertion (and therefore not able to ever validate a password positively).
散列需要 64 个字符,如果您的散列密码在插入时被截断(因此无法肯定地验证密码),您将不会从 laravel 收到错误。
回答by judasane
@eric-evans is correct. To use authentication in laravel 4, you must make your "user" model uses the \Illuminate\Auth\UserInterface interface, and implements the methods
@eric-evans 是正确的。要在 Laravel 4 中使用身份验证,您必须使您的“用户”模型使用 \Illuminate\Auth\UserInterface 接口,并实现这些方法
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
回答by Eric Evans
Could you show code for your model? These are some things that should be in the User model in order for Auth to work.
你能展示你的模型的代码吗?这些是应该在 User 模型中的一些东西,以便 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;
}
}
回答by lscott3
I was having some trouble with this as well. What I noticed is that in my User model I had:
我也遇到了一些麻烦。我注意到的是,在我的 User 模型中,我有:
protected $softDelete = true;
In the database I had the deleted_at column, but it defaulted with a zeroed out timestamp - 0000-00-00 00:00:00. This was causing the record to not be found and in turn causing the authentication to fail.
在数据库中,我有deleted_at 列,但它默认使用归零时间戳 - 0000-00-00 00:00:00。这导致找不到记录,进而导致身份验证失败。
I had to fix the migration to have the deleted_at column properly created like so:
我必须修复迁移才能正确创建 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();
});
}
Here are the docs on soft delete: http://laravel.com/docs/eloquent#soft-deleting
以下是有关软删除的文档:http: //laravel.com/docs/eloquent#soft-deleting
回答by Petraeus
The Auth class requires a column called id as your primary key in your users table.
Auth 类需要一个名为 id 的列作为用户表中的主键。
回答by Yassine Abdul-Rahman
Note that you User table , the password field is Hashed, cause of Auth::attempt($credentials); $credentials->password evaluate the hashed password
请注意,您的 User 表,密码字段是 Hashed,原因是 Auth::attempt($credentials); $credentials->password 评估散列密码
回答by Vaibhavraj Roham
Check does your password is hashed. It should not be normal text..
检查您的密码是否经过哈希处理。应该不是普通文字。。
回答by Neo Ighodaro
Your code is bugging out because you are passing the wrong array keys to Auth::attempt()
. That method requires an array with keys username, password and optionally remember. In that light, your above code should be:
您的代码出了问题,因为您将错误的数组键传递给Auth::attempt()
. 该方法需要一个包含用户名、密码和可选记住的键的数组。有鉴于此,您上面的代码应该是:
Route::post('login', function()
{
$credentials = [
'username' => '[email protected]',
'password' => 'superSecretPassword'
];
dd(Auth::attempt($credentials));
});