Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined array key "password" in LoginController (Laravel 8)

I am trying to customize my login but it throws me the error that appears in the title.

This is the function in the LoginController:

public function login(Request $request) {
  $request->validate([
    'username' => 'required|string|email',
    'password' => 'required|string',
    'remember' => 'boolean',
  ]);

  #:: Undefined array key "password"
  if ($this->guard()->attempt(['a_username' => $request->username, 'a_password' => $request->password], $request->has('remember'))) {
    return $this->sendLoginResponse($request);
  }

  return $this->sendFailedLoginResponse($request);
}

The question why I use these custom columns and not the ones that come with laravel by default. Is that I have 2 tables that are used to authenticate.

In my database I have the columns: a_username (it is the email) a_password (is the password)

This is the login form:

<form class="js-validation-signin" action="{{ route('login') }}" method="POST">
  @csrf
  <div class="form-group">
    <input type="email" class="form-control form-control-lg form-control-alt py-4" name="username" placeholder="Username" value="{{ old('username') }}">
  </div>
  <div class="form-group">
    <input type="password" class="form-control form-control-lg form-control-alt py-4" name="password" placeholder="Password">
  </div>
</form>

Thank you for your patience as I have to translate this problem from Spanish to English. :)

like image 751
Elian Miranda Avatar asked Sep 20 '25 10:09

Elian Miranda


1 Answers

Eloquent user provider contain this code

public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password'];
   
    return $this->hasher->check($plain, $user->getAuthPassword());
}

So to override default functionality you need:

  1. In your user model add (override) method getAuthPassword
public function getAuthPassword()
{
    return $this->a_password;
}
  1. Update your code (change a_password to password):
 if ($this->guard()->attempt(['a_username' => $request->username, 'password' => $request->password], $request->has('remember'))) {
    return $this->sendLoginResponse($request);
  }
like image 129
yaroslawww Avatar answered Sep 22 '25 10:09

yaroslawww



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!