I created a table fields with costume fields names fld_ID, fld_Username, fld_Password but i can't use this fields type on simple laravel Authentication, so i defined my ID field name on user model except laravel-4:
protected $primaryKey = 'fld_ID';
and for Password name:
public function getAuthPassword() {
return $this->attributes['fld_Password'];
}
and finally for my username on Post Action Defined a username type for attempt:
$input = Input::all();
$user['fld_Username'] = $input['fld_Username'];
$user['fld_Password'] = $input['fld_Password'];
if (Auth::attempt($user)){
....Some Code Here... :)
}
but still i have problem with and Auth::attempt return false, my last Query log is this:
Array ( [query] => select * from `tbl_users` where `fld_Username` = ? and `fld_Password` = ? limit 1 [bindings] => Array ( [0] => username [1] => password )
and password is Hashed before save.
Because you changed the password field's name, you need to make a custom user provider and register it in config file.
As you can see in vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php line 138 the password field is hard coded and there is no other way to change it.
So make a class somewhere is app folder like this:
class CustomUserProvider extends EloquentUserProvider
{
public function retrieveByCredentials(array $credentials)
{
if (isset($credentials['fld_Password'])) {
unset($credentials['fld_Password']);
}
return parent::retrieveByCredentials($credentials);
}
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['fld_Password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
Now you need to introduce this user provider to laravel. just add this to app\Providers\AuthServiceProvider.php:
class AuthServiceProvider extends ServiceProvider
//...
public function boot()
{
\Auth::provider('CustomUserProvider', function ($app, array $config) {
return new CustomUserProvider($app['hash'], $config['model']);
});
}
//...
}
Just one more step. in config/auth.php edit the providers section like this:
'providers' => [
'users' => [
'driver' => 'CustomUserProvider', // make sure you change this
'model' => CustomUserModel::class, // here is your custom model
],
],
I hope this will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With