Mehdi Hosseini
Mehdi Hosseini

Reputation: 2147

Laravel custom authentication user fields

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.

Upvotes: 3

Views: 4597

Answers (3)

Mehdi
Mehdi

Reputation: 756

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.

Upvotes: 1

Mehdi
Mehdi

Reputation: 756

Simply override the username function from LoginController and return your username field, just like this:

class LoginController extends Controller
{
    .
    .
    .
    public function username()
    {
        return "fld_Username";
    }
}

No need to change anything else except the getAuthPassword method that you just did correctly.

I hope this will help you.

Upvotes: 0

Hao Luo
Hao Luo

Reputation: 1891

The array you pass to attempt is simply an array; it has nothing to do with your database table. you don't have to use 'fld_Username', and 'fld_Password'. Instead just use $user['username'] and $user['password'].
Laravel is looking for the word "password" as the key in the array you pass to see which is the password.

(Proof)

Upvotes: 0

Related Questions