Reputation: 304
I am trying to create a login page where I am checking for the password. This method was working a while ago but now it is giving me the following error:
Trying to get property 'password' of non-object
The problem is in the retrieving and checking of the hashed password. However, I cannot find the error. Could someone help me out. My code is as follows:
public function logs_in(Request $request){
$email = $request->input('email');
$password = $request->input('password');
$hashedPassword = User::where('email', $email)->first();
if(Hash::check($password, $hashedPassword->password)){
$request->session()->put('success');
return redirect()->route('admin');
} else {
return redirect()->route('login')->with('login_error', 'Invalid
credentials entered');
}
}
Upvotes: 2
Views: 6946
Reputation: 163788
This means there is no user with given email in DB and the result of this query is null
:
User::where('email', $email)->first()
So, change the code to:
if ($hashedPassword && Hash::check($password, $hashedPassword->password)) {
Or use the optional()
helper:
if (Hash::check($password, optional($hashedPassword)->password)) {
Upvotes: 5