Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Laravel 5.4 password encryption and table column names

I am trying to integrate the auth in laravel 5.4 within an existing database where the user and password fields have other names (memberid, passwordnew_enc). With the bellow changes and forcing the create function in RegisterController to use MD5 I managed to make the registration work. It also logins fine after registration. However the actual login form returns:

These credentials do not match our records.

So far I have changed the User.php

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

and

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = md5($value);
}

Also on LoginController.php

public function username()
{
    return 'memberid';
}

Did I miss something ?

I only need to change the two column names to fit and the password encryption from bcrypt to md5

like image 204
dev Avatar asked Mar 09 '17 20:03

dev


1 Answers

I would make custom user provider php artisan make:provider CustomUserProvider:

<?php

namespace App\Providers;

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class CustomUserProvider extends EloquentUserProvider {

    /**
    * Validate a user against the given credentials.
    *
    * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
    * @param  array  $credentials
    * @return bool
    */
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password']; // will depend on the name of the input on the login form
        $hashedValue = $user->getAuthPassword();

        if ($this->hasher->needsRehash($hashedValue) && $hashedValue === md5($plain)) {
            $user->passwordnew_enc = bcrypt($plain);
            $user->save();
        }

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

}

This way if the password exists using md5 it will allow it to work once and then rehash it.


You will register the CustomUserProvider in App\Providers\AuthServiceProvider boot() as follows:

$this->app['auth']->provider('custom', function ($app, array $config) {
            $model = $app['config']['auth.providers.users.model'];
            return new CustomUserProvider($app['hash'], $model);
        });

Edit your config/auth.php

'providers' => [
        'users' => [
            'driver' => 'custom',
            'model' => App\User::class,
        ],
],

You will also need to add the following as mentioned previously...

app\Http\Controllers\Auth\LoginController.php

public function username()
{
    return 'memberid';
}

app\User.php

public function getAuthIdentifierName()
{
    return 'memberid';
}

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

public function getAuthPassword()
{
    return $this->passwordnew_enc;
}
like image 148
upful Avatar answered Oct 10 '22 08:10

upful