I’m encountering an "Undefined array key ‘password’" error when trying to log in a user using Laravel’s Auth::attempt method. Below is the relevant section of my code:
public function register_post(Request $request) {
$request->validate([
'nama_user' => 'required',
'alamat_user' => 'required',
'email_user' => 'required|email|unique:user,email_user',
'notelp_user' => 'required',
'password_user' => 'required|min:6',
]);
$data['nama_user'] = $request->nama_user;
$data['alamat_user'] = $request->alamat_user;
$data['email_user'] = $request->email_user;
$data['notelp_user'] = $request->notelp_user;
$data['password'] = Hash::make($request->password_user);
User::create($data);
$login = [
'email_user' => $request->email_user,
'password_user' => $request->password_user
];
if (Auth::attempt($data)) { // <-- Error occurs here
$request->session()->regenerate();
return redirect()->intended('/login');
}
return redirect()->back()->withInput($request->only('email_user'))->withErrors([
'login_failed' => 'Email atau password salah.',
]);
}
Error Message:
Undefined array key "password"
Additional Details:
The error points to the line where Auth::attempt($data)
is called.
I’m using Laravel’s built-in authentication methods.
The User model uses email_user as the email field and password_user as the password field.
I tried to create a user registration and login system in Laravel. The registration part works fine, and the user is created successfully. However, when I attempt to log the user in immediately after registration using the Auth::attempt method, I encounter an "Undefined array key ‘password’" error.
Here’s what I did:
Validated the request inputs.
Created a new user with the provided data, hashing the password before saving it.
Tried to log in the user using the Auth::attempt method.
My expectation was that the user would be logged in and redirected to the intended URL if the credentials were correct. Instead, I got an error pointing to the line where Auth::attempt is called.
I double-checked the array keys I used for authentication. I expected the authentication attempt to succeed because the user’s credentials should match those stored in the database.
Any guidance on why this error is occurring and how to fix it would be greatly appreciated.
2
Answers
Currently, you are assigning to
$data['password']
an empty or null value. $request is your current request instance, you cannot access form values directly. To do so, use$request->input('password')
;however, it is standard to validate form first, store validated data in another variable, and then use that variable.
Store the validated data in an array and use that array instead of
Hash::make($request->password_user);
. Like:$validated
= $request->validate(['nama_user' => 'required', 'alamat_user' => 'required', 'email_user' => 'required|email|unique:user,email_user', 'notelp_user' => 'required','password_user' => 'required|min:6',]);
And then
The issue happens because the
Auth::attempt
method in Laravel expects the credentials array to have specific keys:email
andpassword
. In your case, however, the keys areemail_user
andpassword_user
.The error occurs because
Auth::attempt
expects an array with keysemail
andpassword
. Map your request data to these keys before callingAuth::attempt
, you can resolve the issue.Update the keys in your
$login
array to match the default keys Laravel expects:Then, use this
$login
array in theAuth::attempt
method: