5

So I found a few problems already which says that you have to override getAuthPassword() to give custom name of password column from database. Tried putting this method with the same name as column in a database and didnt work. It still shoots this error: Undefined index: password.

This is the auth:

if (Auth::attempt(Input::only('user_displayName'), Input::get('user_password')))

Tried changing user_password to password both in form and controller nothing works.

So the question is if I have a column in a database called "user_password" is there a way to make Auth work?

P.S checked every older solution I found

EDIT

Structure of user table:

+======================+
|        User          |
+======================+
|       user_id        |
+----------------------+
|   user_displayName   |
+----------------------+
|     user_fname       |
+----------------------+
|      user_lname      |
+----------------------+
|      user_email      |
+----------------------+
|     user_password    |
+----------------------+
|      created_at      |
+----------------------+
|      updated_at      |
+----------------------+
linktoahref
  • 7,812
  • 3
  • 29
  • 51
DasBoot
  • 469
  • 6
  • 17
  • Please post your migrations or a sql dump of your schema. nevermind read it through again. not required but still could be helpful. – michael.schuett Sep 23 '14 at 18:52
  • You can look at http://stackoverflow.com/questions/26073309/how-to-change-custom-password-field-name-for-laravel-4-and-laravel-5-user-auth/ It does work for me – Marcin Nabiałek Sep 27 '14 at 09:44

4 Answers4

7

tldr; You can name your password field anything you like, as long as your User model implements the interface correctly.

However you can't pass different array key to the Auth::attempt method - only password index can be there

First off you're doing it wrong - you need to pass an array of credentials as 1st param:

if (Auth::attempt(Input::only('user_displayName', 'user_password')))

Next, unfortunately Eloquent provider has hard-coded password array index in the code, so you can't pass user_password to the attempt method.

So this is what you need:

$credentials = Input::only('user_displayName');
$credentials['password'] = Input::get('user_password');

if (Auth::attempt($credentials))

// or simply rename the input in your form to password and:
if (Auth::attempt(Input::only('user_displayName', 'password')))
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • Correct me if i'm wrong but it does not look like it's hard coded into the function. https://github.com/laravel/framework/blob/4.2/src/Illuminate/Auth/Guard.php#L343 – michael.schuett Sep 23 '14 at 19:09
  • 1
    @mschuett Well, you are wrong ;) It's not `Guard`, It's `Eloquent` provider: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Auth/EloquentUserProvider.php#L106 – Jarek Tkaczyk Sep 23 '14 at 19:14
  • My bad didn't follow the function calls far enough good catch. That seems to me like an oversight in the design and a pretty easy fix. – michael.schuett Sep 23 '14 at 19:17
  • It works. The tutorial I was using didnt even show that the first one has to be an array in the first place. Thank you – DasBoot Sep 23 '14 at 19:17
  • @JarekTkaczyk Are you sure password cannot be changed? I think it can. Please look at http://stackoverflow.com/questions/26073309/how-to-change-custom-password-field-name-for-laravel-4-and-laravel-5-user-auth/ Maybe I'm wrong – Marcin Nabiałek Sep 27 '14 at 09:43
  • @MarcinNabiałek It's not that the field cannot be changed. But you cannot specify anything but `password` in the credentials array passed to the `Guard@attempt` - which is executed when you call `Auth::attempt` – Jarek Tkaczyk Sep 27 '14 at 10:35
  • Could anyone enlighten me here plz? How does the Auth::attempt knows which password field in the table to use? Input::get('user_password'); doesn't really tell Auth which table field to use... or does it? – Raccoon Jan 04 '15 at 17:19
  • It doesn't need to know which table field to use, since it will just use `$user->getAuthPassword()` method. The only defect is that it will look for `$credentials['password']` explicitly. – Jarek Tkaczyk Jan 04 '15 at 17:29
  • 1
    This. Should be stickied. It's not anywhere in the documentation. – Mugluck Oct 28 '16 at 02:14
1

I have not tested this but I believe you just have to override a function in UserTrait.php although don't hack the source. In your models/User.php file add the following function.

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->user_password;
}
michael.schuett
  • 4,248
  • 4
  • 28
  • 39
1

There's a static solution for situations when implementing the given interface is not enough (e.g. you have two password columns, a user password and a support password).

$qry = DB::table('users')
        ->where('email', $email)
        ->whereNotNull('support_password')
        ->first();

if(!empty($qry))
{
    $check = Hash::check($password, $qry->support_password);
    if ($check)
    {
        Auth::loginUsingId($qry->id, true);
    }
}
egekhter
  • 2,067
  • 2
  • 23
  • 32
0

Here is my way of changing the default email field of the Laravel login to 'email_address' without changing the vendor code.

I made a trait class that extends the vendor AuthenticatesUsers trait. And only extended the username method.

App\Http\Controllers\Auth\AuthenticatesLogins.php:

namespace App\Http\Controllers\Auth;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

trait AuthenticatesLogins
{
    use AuthenticatesUsers {
        AuthenticatesUsers::username as parentUsername;
    }

    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        $this->parentUsername();
        return 'email_address';
    }
}

Now in App\Http\Controllers\Controller\LoginController.php:

class LoginController extends Controller
{
    use AuthenticatesLogins; // <-- for custom login fields
    // etc.

And... done! Hope this helps.

Floris
  • 2,727
  • 2
  • 27
  • 47