1

I'm learning about Laravel 4, but now I can't login users into my app. I read multiple tutorials to create a loggin area, but I can't create a session.

SOLUTION: laravel 4 custom named password column

You have to be careful with the "password" index, NO MATTER HOW YOUR TABLE NAMES THE PASSWORD FIELD, it must to be "password" in the Auth::attempt function

The users DB table:

  • idusuario, int(11)
  • login, varchar(45)
  • nombre, varchar(45)
  • contrasena, varchar(255)
  • status, bool
  • remember_token, varchar(100)
  • idperfil, int(11)

This is my autentication function:

Route::post('login', function()
{
    if(Auth::attempt([ 'login' => Input::get('login'), 'contrasena' => Input::get('password') ]))
        return Redirect::intended('home');
    return Redirect::to('login')->with('response','Ingreso invalido');
}

My custom User Model:

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

class Usuario extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'usuario';
    protected $hidden = array('contrasena');
    protected $fillable = array('login', 'nombre', 'status', 'idperfil');
    protected $guarded = array('idusuario', 'contrasena');
    public $primaryKey = 'idusuario';
    public $timestamps = false;

    public function getAuthIdentifier()
    {
        return $this->login;
    }

    public function getAuthPassword()
    {
        return $this->contrasena;
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    public function getReminderEmail()
    {
        return false;
    }

My view:

{{ Form::open(array('url' => 'login', 'role' => 'form')) }}
    <div class="response">{{ $response or '' }}</div>
    <div class="form-group">
        <label for="login">Usuario</label>
        <input type="text" class="form-control" id="login" name="login" placeholder="Ingrese su login">
    </div>
    <div class="form-group">
        <label for="password">Contraseña</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="*********">
    </div>
    <button type="submit" class="btn btn-primary">Iniciar Sesión</button>
</form>

I had been testing with

dd(Auth::attempt([ 'login' => Input::get('login'), 'contrasena' => Input::get('password') ]));

But I can't fix it, but when I test the Hash Check to validate the password it returns true! Some idea to get it? Thanks in advance!

Community
  • 1
  • 1

1 Answers1

0

Sorry, I didn't read the 2nd. line of Jarek Tkaczyk's answer in this post. Make sure to review your index names if you have the same problem.

Community
  • 1
  • 1