Skip to content

ParseUser.BecomeInBackground works on parse.com but not on hosted parse server #2361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Abhishek-Mohan opened this issue Jul 22, 2016 · 2 comments

Comments

@Abhishek-Mohan
Copy link

Abhishek-Mohan commented Jul 22, 2016

Hi, so I used the anyPhone example here for logging in with Twilio, https://github.com/ParsePlatform/AnyPhone

The problem is, on Android, when we get the session token back from the cloud code and try on Android Parseuser.becomeinbackground(token, callback);
The error that we get is com.parse.ParseRequest$ParseRequestException: bad json response
I checked and debugged and saw that the session token is the same session token i have in the database, they are totally the same.
The funny thing is, this worked on Api.parse.com but not on our hosted parse server.
So i am wondering if this is a parse function bug or a bug in the sdk.

Steps to reproduce

Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.

  1. Use AnyPhone example on github, log in to the app with phone number
  2. Use the cloud function "logIn"
Parse.Cloud.define("logIn", function(req, res) {

    var phoneNumber = req.params.phoneNumber;
    phoneNumber = phoneNumber.replace(/\D/g, '');

    if (phoneNumber && req.params.codeEntry) {
        Parse.User.logIn(phoneNumber, secretPasswordToken + req.params.codeEntry).then(function (user) {
            console.log(user.getSessionToken());
            res.success(user.getSessionToken());
        }, function (err) {
            res.error(err);
        });
    } else {
        res.error('Invalid parameters.');
    }
});

It sends back the user session token to android
3. It comes back to android and the user puts the code that was sent via twilio
and this function gets called

private void doLogin() {
        if (mInputText.getText().toString().isEmpty() || mInputText.getText().toString().length
                () != 4) {
            mInputText.setError("4 digits only");
            codeUI();
        }
        else {
            int code = Integer.parseInt(mInputText.getText().toString());
            HashMap<String, Object> params = new HashMap<>();
            params.put("phoneNumber", phoneNumber);
            params.put("codeEntry", code);
            ParseCloud.callFunctionInBackground("logIn", params, new FunctionCallback<String>() {
                public void done(String response, ParseException e) {
                    if (e == null)
                    {
                        Log.d(TAG, "It called this function");
                        Log.d (TAG, response);
                        mToken = response;
                        Log.d ("Cloud Response", "There were no exceptions! " + response);

                        ParseUser.becomeInBackground(mToken, new LogInCallback()
                        {
                            @Override
                            public void done(ParseUser parseUser, ParseException e) {
                                if (parseUser != null)
                                {
                                    Log.d(TAG,"become in background worked");

                                    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
                                    installation.put("user", parseUser);
                                    installation.saveInBackground(new SaveCallback() {
                                        @Override
                                        public void done(ParseException e) {
                                            Intent intent = new Intent(SignupActivity.this, MainActivity.class);
                                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                            startActivity(intent);
                                            finish();
                                        }
                                    });

                                }
                                else
                                {
                                    Log.d(TAG, "parse become in background did not work");
                                    Log.d(TAG, "No response was given");
                                    Log.d("Cloud Response", "Exception: " + e.toString());
                                    Toast.makeText(getApplicationContext(),
                                            "Something went wrong.  Please try again." + e.toString(),
                                            Toast.LENGTH_LONG).show();
                                    phoneNumberUI();
                                }
                            }
                        });
                    }
                    else
                    {
                        phoneNumberUI();
                        Log.d("Cloud Response", "Exception: " + response + e);
                        Toast.makeText(getApplicationContext(),
                                "Something went wrong.  Please try again.",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

It fails at Parseuser.BecomeinBackground, due to the token being a bad json response.

Expected Results

It should run the becomeinbackground and log the user into the app

Actual Outcome

com.parse.ParseRequest$ParseRequestException: bad json response

Environment Setup

  • Server
    • parse-server version: [2.2.16]
    • Operating System: [64bit Amazon Linux 2016.03 v2.1.3 running Node.js]
    • Hardware: [N/A]
    • Localhost or remote server? (AWS)
  • Database
    • MongoDB version: [2.6]
    • Storage engine: [Sandbox]
    • Hardware: [Single database
      on multi-tenanted process]
    • Localhost or remote server? (mLab)

I wonder is it a problem with the Parse Server the function Parse.User.login or is it a problem with the android code, ParseUser.become.
I am not so good with server or database stuff, as i am more android side of things. I will provide as much as I can.

@ranhsd
Copy link
Contributor

ranhsd commented Jul 25, 2016

Hi @Abhishek-Mohan.
i tested it on my side and everything works well.
This is my android code:

        ParseCloud.callFunctionInBackground("login",map, new FunctionCallback<Object>() {

            @Override
            public void done(Object object, ParseException e) {
                if (object != null){
                    String token = (String) object;
                    ParseUser.becomeInBackground(token, new LogInCallback() {
                        @Override
                        public void done(ParseUser user, ParseException e) {
                            if (user != null){
                                Log.i("login",user.getEmail());
                            }
                        }
                    });
                }
            }
        });

and in my cloud code i am calling the logIn function exactly like you did.
the only difference is that in the callFunctionInBackground callback function i received an Object and you receive a String

@hramos
Copy link
Contributor

hramos commented Sep 6, 2016

It's not clear this is an issue, happy to reopen if I'm wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants