title: Authentication in jQuery Mobile and PhoneGap
body: <p>I have a web application built with jQuery Mobile and PHP (CodeIgniter framework). Now I'm trying to make a PhoneGap version of it as well, to make it distributable as a standalone app. However, the PHP web app. version uses Ion Auth, a CodeIgniter plugin for authentication. So when you go to a page that requires authentication, the app redirects you to the authentication controller login method. And after authentication it redirects you back to the home page (the jQuery Mobile page in this case). This works fine in the web app., since the home page is opened by the home controller in the first place anyway.</p>
  
  <p>But here's the crux: in the PhoneGap version, the "home" page needs to be the index.html file in PhoneGap. Apparently you can load another url on startup by adding a value in PhoneGap.plist, but that is not acceptable by apple for submitting to app store. And if I do a redirect in the authentication, I can't get back to the index.html file after authentication...</p>
  
  <p>So how should one go about authentication in a PhoneGap/jQuery Mobile app?</p>
  
  <p>UPDATE:</p>
  
  <p>I have tried this according to one of the answers, but the app still tries to navigate to the account/login page (which doesn't exist), when I just want to login through the post and return a value from the method:</p>
  
  <pre><code>    $('#login_form').bind('submit', function () {
          event.preventDefault();
          //send a post request to your web-service
          $.post('http://localhost/app_xcode/account/login', $(this).serialize(), function (response) {
              //parse the response string into an object
              var response = response;
              //check if the authorization was successful or not
              if (response == true) {
                  $.mobile.changePage('#toc', "slide");
              } else {
                  alert('login failed');
                  $.mobile.changePage('#toc', "slide");
              }
          });
      });
  </code></pre>
  
  <p>Here's the controller method:</p>
  
  <pre><code>function login()
      {
          //validate form input
          $this-&gt;form_validation-&gt;set_rules('identity', 'Identity', 'required');
          $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required');
  
          $base_url = $this-&gt;config-&gt;item('base_url');
  
          $mobile = $this-&gt;detect_mobile();
          if ($mobile === false &amp;&amp; $base_url != 'http://localhost/app_xcode/') //Only restrict if not developing
              redirect('account/notAMobile');
  
          else if ($this-&gt;form_validation-&gt;run() == true) { //check to see if the user is logging in
              //check for "remember me"
              $remember = (bool)$this-&gt;input-&gt;post('remember');
  
              if ($this-&gt;ion_auth-&gt;login($this-&gt;input-&gt;post('identity'), $this-&gt;input-&gt;post('password'), $remember)) { //if the login is successful
                  //redirect them back to the home page
                  $this-&gt;session-&gt;set_flashdata('message', $this-&gt;ion_auth-&gt;messages());
  
                  echo true;
                  /*redirect($this-&gt;config-&gt;item('base_url'), 'refresh');*/
              }
              else
              { //if the login was un-successful
                  //redirect them back to the login page
                  $this-&gt;session-&gt;set_flashdata('message', $this-&gt;ion_auth-&gt;errors());
                  /*redirect('account/login', 'refresh');*/ //use redirects instead of loading views for compatibility with MY_Controller libraries
              }
          }
          else
          { //the user is not logging in so display the login page
              //set the flash data error message if there is one
              $this-&gt;data['message'] = (validation_errors()) ? validation_errors()
                      : $this-&gt;session-&gt;flashdata('message');
  
              $this-&gt;data['identity'] = array('name' =&gt; 'identity',
                                              'id' =&gt; 'identity',
                                              'type' =&gt; 'text',
                                              'value' =&gt; $this-&gt;form_validation-&gt;set_value('identity'),
              );
              $this-&gt;data['password'] = array('name' =&gt; 'password',
                                              'id' =&gt; 'password',
                                              'type' =&gt; 'password',
              );
  
          }
      }
  </code></pre>
  
  <p>I think I have removed or commented out any redirects that were there. So I don't know why it tries to load the view still? Does it have something to do with jQuery Mobile trying to navigate there because I post to that url?</p>