简体   繁体   English

Laravel 4 Auth:尝试不起作用

[英]Laravel 4 Auth:attempt not working

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' => 'admin@admin.com','password' => 'admin');
if(Auth::attempt($userData)){
    // redirect
}
else{
   echo 'Invalid';
}

And it returns Invalid everytime 并且每次都返回无效

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',
),
 );
 ?>

Make sure that your password field in your db has space for 64 characters . 确保数据库中的密码字段具有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中得到错误提示。

@eric-evans is correct. @ eric-evans是正确的。 To use authentication in laravel 4, you must make your "user" model uses the \\Illuminate\\Auth\\UserInterface interface, and implements the methods 要在laravel 4中使用身份验证,您必须使您的“用户”模型使用\\ Illuminate \\ Auth \\ UserInterface接口,并实现方法

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

Could you show code for your model? 您可以显示模型的代码吗? These are some things that should be in the User model in order for Auth to work. 为了使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;
    }
}

I was having some trouble with this as well. 我也遇到了麻烦。 What I noticed is that in my User model I had: 我注意到的是,在用户模型中,我有:

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. 在数据库中,我有delete_at列,但默认情况下它的时间戳为零-0000-00-00 00:00:00。 This was causing the record to not be found and in turn causing the authentication to fail. 这导致找不到记录,进而导致身份验证失败。

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

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

Note that you User table , the password field is Hashed, cause of Auth::attempt($credentials); 请注意,您的User表中,密码字段为Hashed,原因为Auth :: attempt($ credentials); $credentials->password evaluate the hashed password $ credentials-> password评估哈希密码

Check does your password is hashed. 检查您的密码是否散列。 It should not be normal text.. 它不应该是普通文本。

Your code is bugging out because you are passing the wrong array keys to Auth::attempt() . 您的代码正在调试,因为您将错误的数组键传递给Auth::attempt() That method requires an array with keys username, password and optionally remember. 该方法需要一个包含键用户名,密码和可选记忆的数组。 In that light, your above code should be: 因此,您的上述代码应为:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM