Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danjavia's avatar

Undefined index: password

I try to authenticate using a custom table from existing database. Theri name is TG_users and the fields I use for the authentication are tg_user_name and tg_user_pass.

But qhen I try to login using Auth::attempt( $credentials ) ever launch the follow error.

Undefined index: password

Some idea?

0 likes
12 replies
baldaweb's avatar

Rename the column tg_user_pass password.

It will be best to do

1 like
pmall's avatar

You have to specify your table name in config/auth.php

1 like
sid405's avatar
sid405
Best Answer
Level 27

@danjavia Modify your AuthController to this:

<?php

namespace App\Http\Controllers\Auth;

use Area\Models\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'tg_user_name' => 'required|max:255|unique:users',
            'tg_user_pass' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'tg_user_name' => $data['tg_user_name'],
            'tg_user_pass' => bcrypt($data['tg_user_pass']),
        ]);
    }

 protected function getCredentials(Request $request)
    {
        return $request->only($this->loginUsername(), 'tg_user_pass');
    }

public function loginUsername()
    {
        return property_exists($this, 'tg_user_name') ? $this->tg_user_name : 'tg_user_name';
    }
}

And make sure your login form is posting tg_user_name as username and tg_user_pass as password.

Hope this helps to point you on the right way

s.

pmall's avatar

i've read somewhere that Laravel guess what field is the password one by looking for a field name containing "password'. If it is true it wont work with tg_user_pass.

bugsysha's avatar

If you do not want to change table/column name then maybe best approach is to create custom driver for your authentication feature.

Change 'driver' property in config/auth.php.

Maybe you'll need also to change 'model' property in config/auth.php to the name of the custom model that you have for that table.

Create app/Acme/Login/AcmeLoginUserProvider.php.

<?php

namespace App\Acme\Login;


use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Support\Facades\Auth;

class AcmeLoginUserProvider implements UserProvider {

    /**
     * AcmeLoginUserProvider constructor.
     */
    public function __construct()
    {
    }

    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        dd($identifier, 'retrieveById');
    }

    /**
     * Retrieve a user by their unique identifier and "remember me" token.
     *
     * @param  mixed  $identifier
     * @param  string $token
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        dd($identifier, $token, 'retrieveByToken');
    }

    /**
     * Update the "remember me" token for the given user in storage.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     * @param  string                                     $token
     *
     * @return void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
        dd($user, $token, 'updateRememberToken');
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     *
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        dd($credentials, 'retrieveByCredentials');
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     * @param  array                                      $credentials
     *
     * @return bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        dd($user, $credentials, 'validateCredentials');
    }
}

Create app/Acme/Login/AcmeLoginServiceProvider.php.

<?php

namespace App\Acme\Login;


use Illuminate\Support\ServiceProvider;

class AcmeLoginServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        // TODO: Implement register() method.
    }

    public function boot()
    {
        $this->app['auth']->extend('acmeLogin', function() {
            return new AcmeLoginUserProvider;
        });
    }
}

Notice that where are dd() calls, that is only for you to know what you need to implement when you are trying to authenticate.

pmall's avatar

@bugsysha No need to make a full auth provider. I think he only needs to override the getAuthPassword method of the Authenticable trait :

# In user model

public function getAuthPassword () {

    return $this->tg_user_pass;

}

Then :

Auth::attempt(['tg_user_name' => $request->username, 'password' => $request->password]);
bugsysha's avatar

Don't think that is OK with second principle of SOLID.

pmall's avatar

Don't think that is OK with second principle of SOLID.

It is ok with the principe of "dont rewrite a full auth provider just for changing the password field name" ^^

3 likes

Please or to participate in this conversation.