I am trying to make user authentication with CodeIgniter and jQuery. I have managed to do the following: If user type wrong user name or password, he remain on the login page and with use of jquery, he get notification that he entered wrong user name or password.
My problem is on success, that is if he type correct user name and password, I manage to send the user on the login page with simple javascript redirect, but that is not what i want. If I use simple redirect with javascript, i can’t fill the user session data, which I need to track the user after the login on all other pages.
I would like if that is possible, if user enter correct user name and password, jquery to do nothing, and function to be activated only in case that user enter wrong user name or password, if that is possible.
Here is the code that I have. JS code first:
formLogin.on('submit', function(e) {
e.preventDefault();
$.ajax({
type : 'POST',
url : 'loginCheck',
data : formLogin.serialize(),
dataType : 'json',
success : function(r) {
if (r.status) {
// this works, but I would like this part to do nothing
// just to leave my PHP controller to redirect the user
window.location = 'http://www.linkedin.com';
} else {
$('#errorMessageTop').fadeIn();
}
}
});
});
And the controller code is:
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('username', $this->input->post('username'));
$ids[1] = $this->db->where('password', md5($this->input->post('password')));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'username' => $this->input->post('username'),
'isUserLoggedIn' => true
);
$isAuthenticated = true;
if ($isAuthenticated) {
$return['status'] = true;
$return['message'] = 'You have successfully been logged in!';
exit(json_encode($return));
$this->session->set_userdata($data);
$data['title'] = "Welcome to dashboard!";
$data['main_content'] = 'dashboard';
$this->load->vars($data);
$this->load->view('backOffice/template');
}
} else {
$return = array(
'status' => false,
'message' => 'Wrong Username or Password'
);
exit(json_encode($return));
}
} else {
$errorMessage = "Second Message Wrong username or pwd...!";
$errorMessage = json_encode($errorMessage);
}
} // end of function loginCheck
Any help will be deeply appreciated.
Regards,Zoran
===============
New code from controller:
public function loginCheck()
{
// set the validation rules
$this->form_validation->set_rules('username', 'Username', 'required|trim|encode_php_tags');
$this->form_validation->set_rules('password', 'Password', 'required|trim|encode_php_tags');
$this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
if ($this->form_validation->run() != FALSE)
{
$ids=array();
$ids[0]=$this->db->where('username', $this->input->post('username'));
$ids[1] = $this->db->where('password', md5($this->input->post('password')));
$query = $this->backOfficeUsersModel->get();
if($query)
{
$data = array(
'username' => $this->input->post('username'),
'isUserLoggedIn' => true
);
$this->session->set_userdata($data);
echo json_encode(array("success" => true));
$data['title'] = "Welcome to dashboard!";
$data['main_content'] = 'dashboard';
$this->load->vars($data);
$this->load->view('backOffice/template');
} else {
echo json_encode(array("success" => false, "error" => "Wrong credentials"));
}
} else {
$errorMessage = "Second Message Wrong username or pwd...!";
$errorMessage = json_encode($errorMessage);
}
} // end of function loginCheck
And the js code:
$("#formLogin").submit(function(e){
e.preventDefault();
var username = $(this).find("#username").val();
var password = $(this).find("#password").val();
var obj = {username: username, password: password};
var url = $(this).attr("action");
$.post(url, obj, function(r){
if(r.success) window.location.replace('http://www.linkedin.com');
else $('#errorMessageTop').fadeIn();
}, 'json');
})
Why are you calling
exit()?… That terminates the script and prevents the session from being set. Also, you’re setting$isAuthenticatedtotrueand immediately checking to see if it’strue. Of course it will be.This is a stripped-down version of how I handle it… In my Controller:
And the corresponding jQuery: