I'm trying to authenticate a user through Ajax in Laravel.
public function authenticate(){
$email = Input::get('email');
$password = Hash::make(Input::get('password'));
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Email is not valid!</span>"]);
}
elseif(Auth::attempt(['email' => $email, 'password' => $password])){
return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Logged in</span>"]);
}
else{
return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Email/password is wrong</span>"]);
}
}
Ajax call
$.post(
'/login/authenticate', // location of your php script
{ email: $("#log_email").val(), password: $("#log_password").val()}, // any data you want to send to the script
function( data ){ // a function to deal with the returned information
if(data.login=='false'){
$("#login_response").empty();
$("#login_response").append(data.error_msg);
}
});
But I'm getting the Email/password is wrong
message all the time even if the credentials are good.
I tried if((User::where('email', $email)->where('pasword', $password)->count())==1)
and it worked.
What's wrong in the Auth::attempt()
method?