Laravel: How to Change the Default Auth Password Field Name

Laravel: How can I change the default Auth Password field name?

For having username instead of email, you can overwrite username() in your LoginController.php

/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'username';
}

And for passwd instead of password, you can do define an accessor in your App\User.php

/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->passwd;
}

login.blade.php : Replace email input with username but do not change the name of the input for password.

How to change password field name in Laravel

I solved my problem by doing a custom AuthController. It was easiest for what I wanted to do.

How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication

Information

You can change easy all other fields in database and use them for authentication. The only problem is with password field.

In fact password field is in some way hard coded in Laravel (but not the way many think) so you cannot just pass array as you passed in your question.

By default if you pass array to attempt (and probably other Auth functions like validate or once) if you do it this way:

Auth::attempt(array(
'user_name' => 'admin',
'password' => 'hardpass',
));

default Eloquent driver will run the following query:

select * from `users` where `user_name` = 'admin' limit 1;

After getting this data from database it will compare password you gave with password property for User object that was created.

But if you simply use:

Auth::attempt(array(
'user_name' => 'admin',
'passwd' => 'hardpass',
));

the following query will be run:

select * from `users` where `user_name` = 'admin' and `passwd` = 'hardpass' limit 1;

and no user will be found in database (in passwd you store hashed password). This is because Eloquent removes from query password but use any other data to run query. Also if you try here to use 'passwd' => Hash:make($data['password']) although user will be found, comparing password won't work.

Solution

Solution is quite easy. You need to run Auth::attempt like this:

Auth::attempt(array(
'user_name' => 'admin',
'password' => 'hardpass',
));

As you see you still pass password as key (although this column doesn't exits in users table) because only this way Eloquent driver won't use it for building query.

Now in User model (app/models/User.php) file you need to add the following function:

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

As you see you use here the column that really exists in database: passwd.

Using it this way you can have column with password named anything you want and you can still use default Eloquent driver for it.

Sample data to test

I've created very simple test for it.

You just need to replace your app/routes.php file with the following:

Route::get('/', function () {

if (Auth::check()) {
echo "I'm logged in as " . Auth::user()->user_name . "<br />";
echo "<a href='/logout'>Log out</a>";
} else {
echo "I'm NOT logged in<br />";

Auth::attempt(array(
'user_name' => 'admin',
'password' => 'hardpass',
));

if (Auth::check()) {
echo "Now I'm logged in as " . Auth::user()->user_name . "<br />";
echo "<a href='/logout'>Log out</a>";
} else {
echo "I'm still NOT logged in<br />";
}
}

});

Route::get('/logout', function () {
Auth::logout();
return "You have been logged out";
});

Route::get('/db', function () {

if (!Schema::hasTable('users')) {

Schema::create('users', function ($table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('user_name', 60)->unique();
$table->string('passwd', 256);
$table->rememberToken();
$table->timestamps();
});

DB::table('users')->insert(
[
[
'user_name' => 'admin',
'passwd' => Hash::make('hardpass'),
]
]
);
}

echo "Table users has been created";
});
  1. Create empty database and set connection data in app/config/database.php
  2. Now you can run /db url for example http://localhost/yourprojectname/db just to create users table.
  3. Now you can run / url for example http://localhost/yourprojectname/ - as you see user is logged in even if in users table in database you don't have any password column (data for authentication has been passed as strings without any forms but of course in real application you will add them) . You can run this url once more time - as you see user is still logged so it is working as expected.
  4. If you click on Log out link, you will be logged out

Laravel 5 changes for above

This solution was tested in Larave 4.2.9 (everything as above) and also in Laravel 5. In Laravel5 everything works the same but you need of course edit files in different paths:

  1. User model is in app/User.php file
  2. routes are in app/Http/routes.php file
  3. Database config file is in config/database.php file

Change the default Laravel authentificaton email field to user_email

By default, Laravel uses the email field for authentication. If you would like to customize this, you may define a username method on your LoginController:

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


Related Topics



Leave a reply



Submit