验证在jQuery Mobile的和的PhoneGap [英] Authentication in jQuery Mobile and PhoneGap

查看:141
本文介绍了验证在jQuery Mobile的和的PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用jQuery Mobile和PHP(codeIgniter框架)构建的Web应用程序。现在,我试图做它的一个PhoneGap的版本,以及,使之可分配作为一个独立的应用程序。但是,PHP的Web应用程序。版本使用离子验证,一个codeIgniter插件进行身份验证。所以,当你去要求身份验证的页面,应用程序将您重定向到验证控制器登录方法。和认证后,它重定向你回到主页(在这种情况下,jQuery Mobile的页面)。这在Web应用程序正常工作,因为在主页由本地控制摆在首位开反正。

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.

但这里的症结:在PhoneGap的版本中,家网页需要在PhoneGap的index.html文件。显然,你可以通过添加PhoneGap.plist值在启动时加载另一个网址,但无法接受被苹果提交到应用商店。如果我在认证做一个重定向,我不能回去验证后index.html文件...

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...

那么,如何应在去了的PhoneGap / jQuery Mobile的应用验证?

So how should one go about authentication in a PhoneGap/jQuery Mobile app?

更新:

我有这种根据答案的一个尝试,但应用程序仍试图导航到该帐户/登录页面(不存在),当时我只是想通过邮局登录并从返回一个值方法:

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:

    $('#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尝试导航那里,因为我张贴到该网址是什么?

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?

推荐答案

您可以从您的应用请求您的Web服务(离子验证)。使用jQuery。您的登录会是这个样子:

You can make requests to your web-service (Ion Auth) from your app. with jQuery. Your login would look something like this:

//add event handler to the `submit` event for your login form
$('#login_form').bind('submit', function () {

    //send a post request to your web-service
    $.post('http://my-domain.com/my-auth/auth.php', $(this).serialize(), function (response) {

        //parse the response string into an object
        response = $.parseJSON(response);

        //check if the authorization was successful or not
        if (response.status === 'success') {
            //do login here
        } else {
            //do error here
        }
    });
});

$(本).serialize()将登录表单的数据添加到POST请求。这个例子假设你的web服务将返回JSON。

$(this).serialize() will add the login form's data to the post request. This example assumes your web-service will return JSON.

这篇关于验证在jQuery Mobile的和的PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆