hangvirus
hangvirus

Reputation: 148

Twilio with Parse on Android to do SMS verfication

I am trying to send sms verification using Twilio Cloud Module in Parse. How do I do it. I don't have any accurate tutorial or usage guide for android. How do I send params to the cloud function ?

Upvotes: 4

Views: 521

Answers (1)

Omar Albelbaisy
Omar Albelbaisy

Reputation: 827

You can create two Cloud Function on Parse, one to send the verification code to the user and one to verify the user's phone number.

To get started with Parse cloud functions take a look at this guide https://www.parse.com/docs/js/guide#cloud_modules

After setup the Parse tool, go to main.js file and add the two functions

// Include the Twilio Cloud Module and initialize it
var twilio = require('twilio')('<Your Twilio Account Sid>', '<Your Twilio Auth Token>');

// Define the Cloud Functions

Parse.Cloud.define("sendVerificationCode", function(request, response) {
    var verificationCode = Math.floor(Math.random()*999999);
    var user = Parse.User.current();
    user.set("phoneVerificationCode", verificationCode);
    user.save();

    twilio.sendSms({
        From: "<Your Twilio phone number>",
        To: request.params.phoneNumber,
        Body: "Your verification code is " + verificationCode + "."
    }, function(err, responseData) { 
        if (err) {
            response.error(err);
        } else { 
            response.success("Success");
        }
    });
});

Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
    var user = Parse.User.current();
    var verificationCode = user.get("phoneVerificationCode");
    if (verificationCode == request.params.phoneVerificationCode) {
        user.set("phoneNumber", request.params.phoneNumber);
        user.save();
        response.success("Success");
    } else {
        response.error("Invalid verification code.");
    }
});

After that, in your Android project you can request phone verification as follow

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("sendVerificationCode", params, new FunctionCallback<JSONObject>() {
    public void done(JSONObject response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
             Log.d("Response", "no exceptions! " + response.toString());
             // Code sent successfully you have to wait it or ask the user to enter the code for verification
        } else {
             Log.d("Response", "Exception: " + response.toString() + e);
             Toast.makeText(getApplicationContext(),  "Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show(); 
        }
    }
});

Then verify the code that (received to)/(entered by) the user

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
params.put("phoneVerificationCode", code);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("verifyPhoneNumber", params, new FunctionCallback<String>() {
    public void done(String response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
            token = response;
            Log.d("Response", "no exceptions! " + response);
            ParseUser.becomeInBackground(token, new LogInCallback() {
                @Override
                public void done(ParseUser parseUser, ParseException e) {
                    if (e == null){
                        Log.d("Response", "no exceptions! ");
                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        Log.d("Response", "Exception: " + e);
                        Toast.makeText(getApplicationContext(),"Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            Log.d("Response", "Exception: " + response + e);
            Toast.makeText(getApplicationContext(), "Something wrong.  Please try again.", Toast.LENGTH_LONG).show();
        }
    }
});

Upvotes: 3

Related Questions