Changing Laravel 5.4 password encryption and table column names

前端 未结 3 1833
孤街浪徒
孤街浪徒 2021-02-04 20:35

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

3条回答
  •  借酒劲吻你
    2021-02-04 21:23

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

    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;
    }
    

提交回复
热议问题