Alireza Noori
Alireza Noori

Reputation: 35

How to send verification code back to my application from Parse Cloud after success?

So I'm building a signup procedure that needs the user to verify their phone number by receiving a code by sms. I'm using Parse as the backend system and I'm using Twilio service which comes included in Parse to take care of the sms function. I have been successful in sending the verification code to user's number.

This is my parse cloud code:

var client = require('twilio')('ACb3....', '2b3....');

//Send an SMS text message
Parse.Cloud.define("sendVerificationCode", function(request, response) {

 var verificationCode = Math.floor(Math.random()*999999);

    client.sendSms({
        From: "+61437877758",
        To: request.params.phoneNumber,
        Body: "Your verification code is " + verificationCode + "."
    }, function(err, responseData) {
        if (err) {
          response.error(err);
        } else {
          response.success("Success");
        }
    });
});

This is the code from the app:

HashMap<String, Object> params = new HashMap<String, Object>();
                params.put("phoneNumber", userNumber);
                ParseCloud.callFunctionInBackground("sendVerificationCode", params, new FunctionCallback<String>() {
                    public void done(String result, ParseException e) {
                        if (e == null) {
                            Log.d("Parse", result);
                            Intent i = new Intent(SignupActivity.this, PhoneVerificationActivity.class);
                            startActivity(i);

                        } else {
                            Toast.makeText(SignupActivity.this, "there was a problem with connection", Toast.LENGTH_LONG).show();
                        }
                    }
                });

Now I would like to know how can I send that verification code back to my android app from Parse Cloud after success, so tat I can check the verification code against the code user puts in the EditText

if (err) {
                  response.error(err);
                } else {
*//So the code for sending the verification code back goes here:*
                  response.success("Success");
                }

Do I need to use Json and Rest API?, how can I call and grab this verification code from the app?. I would really appreciate your help. Thanks.

Upvotes: 1

Views: 765

Answers (1)

Fosco
Fosco

Reputation: 38526

One way would be to return it in response.success...

response.success({ status: "success", verificationCode: ... });

Another way, a better way, is to not trust the client with this. Store a record of it on an object on the server... When the user enters the validation code, call back into another function to check if it is valid. An example of this type of system can be seen in this old out-dated GitHub login example: https://github.com/ParsePlatform/CloudCodeOAuthGitHubTutorial/blob/master/cloud/main.js#L116

Upvotes: 2

Related Questions