Laravel 4 Auth:attempt not working

為{幸葍}努か 提交于 2019-12-04 04:53:48

Make sure that your password field in your db has space for 64 characters. 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).

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

public function getAuthIdentifier() {
            return $this->getKey();
  }
public function getAuthPassword() {
            return $this->password;
        }
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.

<?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. 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:

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

The Auth class requires a column called id as your primary key in your users table.

Note that you User table , the password field is Hashed, cause of Auth::attempt($credentials); $credentials->password evaluate the hashed 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(). 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));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!