Skip to content
Advertisement

Laravel 8 Custom Login With Custom Model

Greetings to all I have a problem with my Laravel 8 code I’m getting "ErrorException Undefined index: password" from vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:159

I use custom model and I set up in config/auth.php to use my Client model not a User model and when I try Auth::attempt($credentials) it’s failed and give me that error here is my code

Client.php Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateFoundationAuthUser as Authenticatable;

class Client extends Authenticatable
{
    use HasFactory;

    protected $table = 'clients';
    protected $primaryKey = 'client_id';
    public $incrementing = true;

    protected $fillable = ['client_firstName', 'client_lastName', 'client_email', 'client_phoneNumber', 'client_password', 'client_isAdmin', 'client_created_at', 'client_updated_at'];

    public $timestamps = true;
    const CREATED_AT = 'client_created_at';
    const UPDATED_AT = 'client_updated_at';
}

config/auth.php

 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => AppModelsClient::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

ClientController.php

public function signIn(Request $request){
       if($request->isMethod('POST')){
            $validator = Validator::make($request->all(), [
                'signin_email' => 'required|string|email:rfc,dns|bail',
                'signin_password' => 'required|string|bail',
            ], [
                'signin_email.required' => 'The email address field is required.',
                'signin_email.email' => 'You must provide an a valid email address.',
                'signin_password.required' => 'The password field is required.',
            ]);

            if($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }else{
                $credentials = [
                    'client_email' => $request->signin_email,
                    'client_password' => $request->signin_password,
                ];

                if(Auth::attempt($credentials)) {
                    return redirect()->to('dashboard');
                }else{
                    return redirect()->back()->withErrors("Sorry, the passed email address or password is incorrect, try again!")->withInput();
                }
            }
       }else{
           return view('signIn');
       }
    }

Advertisement

Answer

I have found a solution I will need to add to my model getAuthPassword, also I need to change app/auth.php and ClientController.php

Client.php Model

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateFoundationAuthUser as Authenticatable;

class Client extends Authenticatable
{
    use HasFactory;

    protected $table = 'clients';
    protected $primaryKey = 'client_id';
    public $incrementing = true;

    protected $fillable = ['client_firstName', 'client_lastName', 'client_email', 'client_phoneNumber', 'client_password', 'client_isAdmin', 'client_created_at', 'client_updated_at'];

    public $timestamps = true;
    const CREATED_AT = 'client_created_at';
    const UPDATED_AT = 'client_updated_at';

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

app/auth.php

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'clients',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'clients',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'clients' => [
            'driver' => 'eloquent',
            'model' => AppModelsClient::class,
            'table' => 'clients',
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'clients' => [
            'provider' => 'clients',
            'table' => 'clients',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

ClientController.php

public function signIn(Request $request){
       if($request->isMethod('POST')){
            $validator = Validator::make($request->all(), [
                'signin_email' => 'required|string|email:rfc,dns|bail',
                'signin_password' => 'required|string|bail',
            ], [
                'signin_email.required' => 'The email address field is required.',
                'signin_email.email' => 'You must provide an a valid email address.',
                'signin_password.required' => 'The password field is required.',
            ]);

            if($validator->fails()) {
                return redirect()->back()->withErrors($validator)->withInput();
            }else{
                $credentials = [
                    'client_email' => $request->signin_email,
                    'password' => $request->signin_password,
                ];

                if(Auth::attempt($credentials)) {
                    return redirect()->to('dashboard');
                }else{
                    return redirect()->back()->withErrors("Sorry, the passed email address or password is incorrect, try again!")->withInput();
                }
            }
       }else{
           return view('signIn');
       }
    }
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement