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.