Ir al contenido principal

Auth::attempt siempre retorna falso

Estaba obteniendo un error que parece común entre los usuarios novatos de Laravel. Al usar Auth::attempt este siempre me estaba retornando falso.

Este era el código que usaba:

class UserTableSeeder extends Seeder{
    public function run(){
        User::create(array(
            'username'  => 'admin',
            'email'     => 'test@test.net',
            'name'=> 'Administrator',
            'password' => Hash::make('123456')
        ));
    }
} 

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
    protected $fillable = array('email', 'username', 'password');

 use UserTrait, RemindableTrait;

 /**
  * 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', 'remember_token');

 /**
  * 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;
 }

    public function setPasswordAttribute($value)
    {
        if ( ! empty ($value))
        {
            $this->attributes['password'] = Hash::make($value);
        }
    }

}

Por mas que intentaba el usuario no se autentificaba, y bien pues el problema era que el hash se estaba ejecutando doble, ya que había modificado el valor que se estaba almacenando con esta función:

public function setPasswordAttribute($value)
{
    if ( ! empty ($value))
    {
        $this->attributes['password'] = Hash::make($value);
    }
}

Luego al momento de ejecutar el seed, estaba intentando pasar el password ya convertido en hash, antes de guardar en la bd:
'password' => Hash::make('123456');

Como ya pueden imaginar a estas altura, la solución es simple, pasar el valor del password sin el Hash::make en el seed.

Espero que a alguien mas le sirva y le evite unos cuantos minutos de dolor de cabeza.

Comentarios

Publicar un comentario