2
votes

I recently got into Laravel, and I wanted to create a User-System for my webapp.

I used php artisan make:auth to create the standart Authentication views/controllers of Laravel. Now I wanted to change the column names of the user table. I wanted to rename name to userName, email to userEmail, password to userPassword.

Registration works perfectly fine, Login has the issues though.

After doing exactly as described in http://www.lasselaursen.com/post/changing-standard-fieldcolumn-names-in-laravel-5s-default-users-table with a few small adjustments, for example instead of overriding postLogin in AuthController, i overrode login in LoginController.

When I try to login now I always get the debug window saying: Undefined index: password in ..\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php

I overrode the function getAuthPassword() in class User.php as follows

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

My login function on LoginController looks as follows:

public function login(Request $request)
{
    $this->validate($request, [
        'userEmail' => 'required|email', 'password' => 'required',
    ]);

    $credentials = $request->only('userEmail', 'password');

    if (Auth::attempt($credentials, $request->has('remember')))
    {
        return redirect()->intended($this->redirectTo);
    }

    return redirect($this->loginPath())
        ->withInput($request->only('userEmail', 'remember'))
        ->withErrors([
            'userEmail' => $this->getFailedLoginMessage(),
        ]);
}

I changed the name field in login.blade.php of the mail to userEmail and left the field for password as password.

I feel like I'm missing something really obvious here, but I just cant find it...

If you need more code snippets just hit me up.

1
I suggest you check your blade template first to check if you have all necessary attributes defined for the password field. ie. <input type="password" name="password" placeholder="password"/> name attribute in this case.user3647971
And another note, the stack trace should tell you in which order the code was ran, in other words, it should tell you which of the function calls in above code was ran. This should give you better understanding what's happening.user3647971
As a blind guess, I would say it's the Auth::attempt($credentials... line which gives the error. $credentials probably doesn't include the password field.user3647971
Thanks for the answer @user3647971 ! I checked the stack trace and the error is really originating from the Auth::attempt line, but the password field is included. When I access it in the line above, for example by writing something random like print_r($credentials["password"]), I dont get an error. For me it looks more like I get the error because I override the getAuthPassword() in the User.php and not in the GenericUser.php where the most upper Stack Trace comes from. The lines the error comes from look like this:Paul Rangger
public function getAuthPassword() { return $this->attributes['password']; } While my overridden function would look as shown in the original post above.Paul Rangger

1 Answers

-1
votes

Laravel by default will use the email field for authentication. Since you have customized it to userEmail, you must ALSO define a username method on your LoginController as below:

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