1
votes

I have authentication up and running in CodeIgnitor using Ion Auth, seems to be a great library with all the functionality I need. But I want to create an AJAX login, using jQuery. Basically to enhance the functionality / degrade gracefully when javascript is or is not present.

I have no idea how to do it so that on subsequent pages where Ion Auth checks the logged in status it will all work properly.

Here's what I have so far:

--

CONTROLLER


public function js_login()
    {

        if( $this->ion_auth->login($this->input->post('identity'),
            $this->input->post('password'), 
            $this->input->post('remember')))
        {
            $result = "Login Details Correct";
        }
        else
        {
            $result = "Login Details incorrect";
        }

        $array = array( "result" => $result );
        echo json_encode($array);
    }

--

SCRIPT



     $("#login").submit(function(event) {
            event.preventDefault(); 
            $.post( "/auth/js_login", $(this).serialize(), function(data){
                // Set the cookie somehow
                alert(data.result);
                }, "json");
        });

--

So I basically need to know how to get the session data that is normally set by Ion Auth and set it using jQuery. After that I can redirect to the appropriate page.

1

1 Answers

0
votes

do the session setting when your login succeeds with PHP (in the js_login function) and just redirect the user. It'll automatically be effective on the next page.