Home > other >  Laravel 8 Custom Login With Custom Model
Laravel 8 Custom Login With Custom Model

Time:02-10

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 App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User 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' => App\Models\Client::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');
       }
    }

CodePudding user response:

The problem is in my database field for a password is doesn't call just 'password' it's called 'client_password' and when I try to Auth::attempt() for login it throws me that error that "password index undefined" but when I change my database column for a password from 'client_password' to just 'password' then It's worked, how can I use custom column name for password and pass Auth::attempt()?

  •  Tags:  
  • Related