0
votes

I am creating a new laravel 4 backend based on an existing database. I have then a table named 'FormMembers' that contains email and passwords for my members.

I would like to use Auth:: functions like :

$credentials = array(
    'sEmail' => Input::get('email'),
    'sPassword' => Input::get('password')
);

if (Auth::attempt($credentials)) {
    return Response::json(Auth::user());
} else {
    return Response::json(array('flash' => 'invalid password or email.'), 500);
}

So I changed the table field in auth.php from 'users' to 'FormMembers' and did the same to the Users model. But when calling Auth::user() I got the error :

{"error":{"type":"ErrorException","message":"Undefined index: password","file":"\/home\/www\/accred\/web\/vendor\/laravel\/framework\/src\/Illuminate\/Auth\/EloquentUserProvider.php","line":75}}

Do you have any idea how I could manage it? Thanks

1

1 Answers

1
votes

The eloquent user provider requires a password field in the credentials array. You passed an array with the key sPassword.

You should be able to do

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

Auth::attempt($credentials);

And handle the rest from the User eloquent model. Even if your user table has different columns.

EDIT: The Auth::attempt() function get's handled by the Illuminate's Guard class. This calls the retrieveByCredentials and then the validateCredentials method of the class that implements the UserProviderInterface. By default this is the EloquentUserProvider.

The retrieveByCredentials method uses all except the password-containing keys of your credentials array. It returns one User eloquent model object. So use your 'sEmail' key there.

The validateCredentials method that get's called on the eloquent user provider checks if the hash of the password field in the credentials array matches with the hashed password retrieved from the getAuthPassword method on your User object, wich it needs to implement, as it implements the UserInterface.

So, you should overwrite the getAuthPassword method on your User class.

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

Like this it returns the hashed password stored in the DB.

Ofcourse your passwords should be hashed in the same way as Hash::make does. If not:

  1. Let all your users reset their password
  2. reimplement the BcryptHasher