3

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.

2
  • idk, but you are missing a closing parenthesis after Auth::attempt to close the if. Commented Jan 14, 2014 at 7:41
  • that's not the problem i edited that line, tnx. Commented Jan 14, 2014 at 7:44

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

4 Comments

i shouldn't change them, by the way, there are many developers who want to have a custom field name stackoverflow.com/questions/17222734/…, and forums.laravel.io/viewtopic.php?pid=49835 and etc...
Ok, then you need to extend the UserProviderInterface, and swap your implementation with the default EloquentUserProvider. Don't forget to register your auth driver. That might seem like killing a fly with a cannon, but you gotta do what you gotta do. But really, that fly is just a dust particle to most people, if you know what I mean.
(See Here for extending Auth) You are right in that Auth retrieves the user based on the key of your credential array while filtering out password.
0

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.

1 Comment

opppps 4 sal pish in soalo porsidi :DDD sry nadidam :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.