1

I have to change the 'users' table to a different table name as per our organization standard. It has different column name for password. So my users table will be 'User' and password column name will be 'encryptedPassword' to match the org standards.

I do not have any migration that refers to users table. Instead, I use User table as a base table for login/registration etc.

Please note that I am using following User Provider : /vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php

I am using Laravel5.4 and want to use default LoginController and RegisterController. So I made following changes to ensure that my code still works with the new user table and password column name.

(1) For registering user:

(1.1) Http/Controllers/Auth/RegisterController.php

  • In the create() method changed the 'password' to 'encryptedPassword' to reflect the modified column name for password. (please note I am not making any changes to blade template for Login UI)
$user = User::create([
   'name' => $data['name'],
   'email' => $data['email'],
   'encryptedPassword' => bcrypt($data['password'])
 ]);

(1.2) User.php ( Eloquent Model for user registration and login functionality) - Added following variable to indicate custom table name for User and custom password column

protected $table = 'User';

protected $primaryKey = 'userId';

protected $encryptedPassword = 'encryptedPassword';

protected $fillable = [
        'name', 'email', 'encryptedPassword',
    ];

protected $hidden = [
        'encryptedPassword', 'remember_token',
    ];

Also added a method to return correct password column

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

After this Registering the User is NOT working in laravel 5.4

The error message is :

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'homestead.users' doesn't exis

( I am using homestead as my DB name)

(2) Login User functionality.

But this is not yet working with the following changes I thought I am supposed to do.

(2.1) Http/Controllers/Auth/LoginController.php

Added following :

protected $encryptedPassword = 'encryptedPassword';

public function credentials(Request $request)
{ return $request->only($this->username(), $this->password());
}

Added this one also:

 protected function password() {

       return $this->encryptedPassword;

   }

But Login is not working as expected.

On login screen I see the error message under Email-Address that

'These credentials do not match our records.'

What am I missing?

Andy
  • 2,493
  • 6
  • 37
  • 63
  • 1
    Can you manually log a user in using the following method: `Auth::attempt(['email' => $email, 'encryptedPassword' => $password])` in Tinker or somewhere? – Yat23 Aug 07 '17 at 21:00
  • If I try that using tinker ... then I get following error : Undefined index: password in /var/www/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php on line 126 – Andy Aug 07 '17 at 21:21
  • EloquentUserProvider.php use a hardcoding to read password in line#126 – Andy Aug 07 '17 at 21:24
  • hi @Andy, is it not possible to [create your own user provider](https://laravel.com/docs/5.4/authentication#adding-custom-user-providers) and jack it to the providers? also, you might suggest people in the IRC to not hardcode the password field.. they always listening SOF question tagged laravel.. – Bagus Tesa Aug 07 '17 at 22:45
  • @BagusTesa Yes. But I was hoping to check if it is possible to change the users table to something else with default EloquentUserProvider with some hack. Some folks have appeared to be done this in prior version of laravel5.4. But so far as per my testing it is not possible to do the same in Laravel5.4. – Andy Aug 07 '17 at 23:03
  • Possible duplicate of [Laravel: How can i change the default Auth Password field name](https://stackoverflow.com/questions/39374472/laravel-how-can-i-change-the-default-auth-password-field-name) – julianstark999 Aug 08 '17 at 05:28
  • @DestinatioN The link you provided is about 11 months old. And it does not tag which version of Laravel he is referring? Also he is not changing the users base table. I am changing users table, primary key and password for user authentication and I have clearly mentioned which driver I am using and which user provider I am using. All this info is missing in the another link. – Andy Aug 08 '17 at 17:13

0 Answers0