0

I'm trying to do a basic authentication which considers email and password. My problem comes when I call the method Auth :: attempt I get the following error.

enter image description here

Model

class Usuario extends Eloquent{
    protected $table = 'Usuario';
    protected $primaryKey = 'idUsuario';
    protected $fillable = array('Nombre', 
                            'Apellido', 
                            'TipoUsuario', 
                            'Contrasena', 
                            'Correo', 
                            'Telefono');
}

Controller

class UsuarioController extends BaseController{
    public function doLogin(){
        $rules = array('correo' => 'required|email',
                        'contrasena' => 'required');

        $validator = Validator::make(Input::all(), $rules);
        if($validator->fails()){
            return Redirect::to('usuario')
                    ->withErrors($validator)// manda los errores al login
                    ->withInput(Input::except('contrasena')); //

        }else{
            $userData = array(
                        'Correo' => Input::get('correo'),
                        'Contrasena' => Input::get('contrasena')
                        );

            if(Auth::attempt($userData)){
                echo 'bien';
            }else{
                return Redirect::to('login');
            }
        }
    }

    public function showLogin(){
        return View::make('login');
    }
}

Routte

Route::get('usuario', array('uses' => 'UsuarioController@showLogin'));
Route::post('usuario', array('uses' => 'UsuarioController@doLogin'));

Auth.php

return array(
    'driver' => 'database',
    'model' => 'User',
    'table' => 'Usuario',
    'reminder' => array(
        'email' => 'emails.auth.reminder',
        'table' => 'password_reminders',
        'expire' => 60,
    ),
);

2 Answers 2

1

In the process of checking user credentials Laravel calls validateCredentials method when Auth::attempt gets called and in this function (given below) Laravel checks for the password key in the passed array and in your case you are not passing a password key so the error happens.

public function validateCredentials(UserInterface $user, array $credentials)
{
    $plain = $credentials['password'];

    return $this->hasher->check($plain, $user->getAuthPassword());
}

Change the key in the $userData array:

$userData = array(
    'email' => Input::get('correo'), // If correo means email
    'password' => Input::get('contrasena') // If contrasena means password
);

Also make changes in your database table's field names which represents users table, I think it's your Usuario table.

Sign up to request clarification or add additional context in comments.

2 Comments

if I change Correo for email apper SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from `Usuario` where `email` = [email protected] limit 1), but if I quick it the same way it work but if I change Contrasena for password apper a new error /** * Get the password for the user. * * @return string */ public function getAuthPassword() { return $this->attributes['password']; } in C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php
Did you read the last line in my answer ? Also make changes in your database table's field names which represents users. You need to change the field names in your Usuario table as well otherwise there is no such a column/field.
1

I'd check to make sure you're passing the correct info to Auth::attempt() in your controller. I use something more like:

$userData = array('email' => Input::get('email'), 'password' => Input::get('password'));

1 Comment

If I do that apper SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from `Usuario` where `email` = [email protected] limit 1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.