简体   繁体   中英

How to sign up/login a user using phonegap + quickblox?

I have been been working on an app in phonegap, wanting to make calls to the quickblox backend service.

The app successfully gets the app token and creates an API session but the user.create code fails - here is my code from index.html

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title></title>
    <!--  -->
    <link rel="stylesheet" href="css/themes/.css" />
    <link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile.structure-1.4.3.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/quickblox.js"></script>
</head>
<body>
    <div style="width:100%;">
        <h1 style="text-align:center;">The Activity Network</h1>
    </div>
    <div>
        <div data-role="tabs" id="tabs">
            <div data-role="navbar">
                <ul>
                    <li><a href="#one" data-ajax="false">Login</a></li>
                    <li><a href="#two" data-ajax="false">Register</a></li>
                </ul>
            </div>
            <div id="one">
                <h2>Login</h2>
                <input type="text" id="new_login" placeholder="Username" />
                <input type="password" id="new_password" placeholder="Password" />
                <input id="auth" type="submit" value="Submit" onclick="window.location='sportsSelector.html'" />
            </div>
            <div id="two">
                <h2>Register</h2>
                <input type="text" id="login" placeholder="Username"/>
                <input type="email" id="email" placeholder="E-Mail Address"/>
                <input type="email" id="confirmEmail" placeholder="Confirm E-Mail Address"/>
                <input type="password" id="password" placeholder="************"/>
                <input type="password" value="confirmPassword" placeholder="************"/>
                <input id="register" type="submit" value="Submit" />
            </div>
        </div>
     </div>
    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>

    <script src="scripts/index.js"></script>
    <script>
        $(document).ready(function () {
            QB.init(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);
            //console.log(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);
            QB.createSession(function (err, result) {
                console.log(result);
                // callback function
            });
        });

        var username;
        var password;
        var email;

        $('#register').click(function () {
            console.log("Ready! Getting the user details.");

            username = document.getElementById('login').value;
            password = document.getElementById('password').value;
            email = document.getElementById('email').value;

            console.log("Username: ", username);
            console.log("Password: ", password);
            console.log("E-Mail: ", email);

            var params, connection;

            params = {
                login: username,
                password: password,
                email: email
            };

            //console.log("Params: ", params);

            create(params);
        });

        function create(params) {
            console.log("Creating new user");
            QB.users.create(params, function (err, result) {
                console.log("Result: ", result);
                // callback function
            });
        }
    </script>
</body>
</html>

If anyone can spot what Im missing then I'd really appreciate it. Currently, it spits out the following in the console:

    XHR finished loading: POST "http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=https%3A//users.quickblox.com/users.json".
[DEBUG] Add new user: POST https://users.quickblox.com/users.json?user[owner_id]=&user[login]=Username&email[email]=undefined&user[password]=Password&token=undefined index.html:197

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=https%3A//users.quickblox.com/users.json

I think one of your other scripts (local) might be interfering. Copied your code to jsfiddle, added some dummy credentials and it worked just fine.

$(document).ready(function () {
    var QBAPP = {
        app_id: replace_with_yours,
        auth_key: replace_with_yours,
        auth_secret: replace_with_yours
    };

    QB.init(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);

    QB.createSession(function (err, result) {
        console.log(result);
        // callback function
    });
});

var username;
var password;
var email;

$('#register').click(function () {
    console.log("Ready! Getting the user details.");

    username = document.getElementById('login').value;
    password = document.getElementById('password').value;
    email = document.getElementById('email').value;

    console.log("Username: ", username);
    console.log("Password: ", password);
    console.log("E-Mail: ", email);

    var params, connection;

    params = {
        login: username,
        password: password,
        email: email
    };

    create(params);
});

function create(params) {
    console.log("Creating new user");
    QB.users.create(params, function (err, result) {
        console.log("Result: ", result);
        // callback function
    });
}

Here is your request:

POST https://users.quickblox.com/users.json?user[owner_id]=1622883&user[login]=Username&email[email]=undefined&user[password]=Password&token=undefined

At least something wrong with email and token:

email[email]=undefined
token=undefined

try to check these parameters carefully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM