Support the ongoing development of Laravel.io →
Laravel Authentication

I am trying to customize the existing login with custom fields. But it is failing always.

Help me in this to solve this issue.

Table Names start with Capital letter eg: Users

Data is coming to login function. i.e. printed the data $request['email'], $request['password'] and checked it.

seems like an issue with the password hash. Tried with the below code and it is working but I wanted to know why Auth::attempt() is not working.

password_verify(Input::get('password'), $user->Password)

LoginController.php

<?php
namespace App\Http\Controllers\Auth;


use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/dashboard';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'Email';
    }

    protected function sendFailedLoginResponse(Request $request)
    {
        $request->session()->put('login_error', trans('auth.failed'));
        throw ValidationException::withMessages(
            [
                'error' => [trans('auth.failed')],
            ]
        );
    }

    public function login(Request $request)
    {
        if (Auth::attempt(['Email' => $request['email'], 'Password' => $request['password']])) {
            echo 'SUCCESS!';
            exit;
        }
        else {
            // validation not successful, send back to form
            echo "Validation not successful";
            exit;
        }
        // Else, the user has been logged in. Do as you wish.
    }

    protected function getCredentials(Request $request)
    {
        return $request->only('Email', 'Password');
    }
}

User.php (Model)


namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;

class User extends Authenticatable
{
    use Notifiable;

    public $timestamps  =   false;
    
    //Set Table Name
    protected $table    =   'Users';
    //Set Primary Key
    public $primaryKey  =   'UserID';

    protected $fillable = [
        'Name', 'Email', 'Password', 'RememberToken', 'CreatedDateTime', 'UpdatedDateTime'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getAuthPassword()
    {
        return $this->Password; // case sensitive
    }
}

Login View:

                        <div class="form-group row">
                            <label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

                                @if ($errors->has('email'))
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                            <div class="col-md-6">
                                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                @if ($errors->has('password'))
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

Table: Users:

UsreID, Name,   Email,            Password,         RememberToken
1       User    user@right.com    $2y$10de....      **********   
Last updated 3 years ago.
0

I have found the solution for this. I am using Laravel 5.8.17.

Auth::attempt() will not work for the custom fields, the reason for this is the provider (Eloquent). vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php.

In the above file go to this function retrieveByCredentials, then you can see the code that password column is static. For testing, you can update that column and try it. But it is not recommended to do it.

So to solve this issue create your custom provider class and use that in config/auth.php to make it work.

People who are using Database provider can see the function retrieveByCredentials in the below file. vendor\laravel\framework\src\Illuminate\Auth\DatabaseUserProvider.php.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.