我有一个使用jQuery Mobile和PHP(CodeIgniter框架)构建的Web应用程序。现在我也想制作一个PhoneGap版本,使其作为独立应用程序分发。但是,PHP Web应用程序版本使用Ion Auth,这是CodeIgniter身份验证插件。因此,当您访问需要身份验证的页面时,应用程序会将您重定向到身份验证控制器登录方法。身份验证后,它会将您重定向回主页(在这种情况下是jQuery Mobile页面)。在Web应用程序中,这很好用,因为主页本来就是由主页控制器打开的。
但是这里的关键是:在PhoneGap版本中,“主页”必须是PhoneGap中的index.html文件。显然,您可以通过在PhoneGap.plist中添加一个值来在启动时加载另一个URL,但是苹果不接受这种方式提交应用商店。如果我在身份验证中进行重定向,则无法在身份验证后返回index.html文件...
那么,如何在PhoneGap / jQuery Mobile应用程序中进行身份验证呢?
更新:
根据其中一个答案,我尝试过这样做,但是当我只想通过发布进行登录并从该方法返回值时,应用程序仍然尝试导航到帐户/登录页面(该页面不存在):
但是这里的关键是:在PhoneGap版本中,“主页”必须是PhoneGap中的index.html文件。显然,您可以通过在PhoneGap.plist中添加一个值来在启动时加载另一个URL,但是苹果不接受这种方式提交应用商店。如果我在身份验证中进行重定向,则无法在身份验证后返回index.html文件...
那么,如何在PhoneGap / jQuery Mobile应用程序中进行身份验证呢?
更新:
根据其中一个答案,我尝试过这样做,但是当我只想通过发布进行登录并从该方法返回值时,应用程序仍然尝试导航到帐户/登录页面(该页面不存在):
$('#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");
}
});
});
以下是控制器方法:
function login()
{
//validate form input
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$base_url = $this->config->item('base_url');
$mobile = $this->detect_mobile();
if ($mobile === false && $base_url != 'http://localhost/app_xcode/') //Only restrict if not developing
redirect('account/notAMobile');
else if ($this->form_validation->run() == true) { //check to see if the user is logging in
//check for "remember me"
$remember = (bool)$this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) { //if the login is successful
//redirect them back to the home page
$this->session->set_flashdata('message', $this->ion_auth->messages());
echo true;
/*redirect($this->config->item('base_url'), 'refresh');*/
}
else
{ //if the login was un-successful
//redirect them back to the login page
$this->session->set_flashdata('message', $this->ion_auth->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->data['message'] = (validation_errors()) ? validation_errors()
: $this->session->flashdata('message');
$this->data['identity'] = array('name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
);
}
}
我认为我已经删除或注释掉了任何重定向。所以我不知道为什么它仍然尝试加载视图?这是否与jQuery Mobile尝试导航到该URL有关,因为我发布到该URL?
response == true
。您正在检查来自服务器端脚本的真/假值,该脚本可能会发送一些您不希望看到的内容(例如错误页面而不是预期的输出)。我建议使用===
来确保您得到完全符合预期的结果。 - Jasper