Twilio Authy & Verify JSON response

#twilio #authy #verify api #wixFetch

Hey guys,

I’m using Twilio Authy & Verify for mobile number verification. User is sent an authentication code on his phone and I want to verify the code using GET after the user receives and enters it on the page.

The Code is correct and working.

I’m receiving the correct message but I’m having trouble with the IF statement.

//Twilio Response

{
  "message":"Verification code is correct.",
  "success":true
}

My Backend Code:

return fetch(url, {
 "method": "GET",
 "headers": headers
    })
    .then(response => response.json());   
}

My Page Code:

export function button2_click(event) {
 const phone = $w("#phone").value;
 const code = $w("#code").value;
 const verificationCode = $w("#verificationCode").value;
 verifyCode(phone, code, verificationCode)
    .then( (json) => {
        react(json);
    });
}

async function react(json) {
    $w("#result").text = await json.message; //this is good
 if(json.success === 'true') { //here is the trouble
        wixLocation.to('/success');
    } else {
        $w("#error").show();
    }
}

As you can see that json.success has to be ‘true’ because my $w(“#result”).text is getting the proper message from the API but still the error keeps showing up instead of the redirect.

I tried converting it to a string like below but its not working

if(String(json.success) === 'true')

Any help?

-Shan

Is the json.success field a Boolean? If so, you need to check for a Boolean value:

You don’t want this:
if(json.success === ‘true’) { //here is the trouble
You do want this:
if(json.success === true) { //here is the trouble

If that doesn’t fix the problem, try a console.log() to see what the contents of the json are so you can check the correct conditional.

This looks cool. Did it work? What other wix code did you use?

Did it work? Can you provide more details?

Good people,

I have been trying to get twilio’s verify API working with the following code, but throws the error :
Can anyone help?=============
===========================Error========================

status:
404
message:
“The requested resource /Services/VA***/Verifications was not found”
code:
20404
moreInfo:
“https://www.twilio.com/docs/errors/20404”
====================================

===================== my code=======================
export function authTest(){

var accountSID = ‘AC2***’;
var authToken = ‘fde***’;

var twilio = require (‘twilio’);
var client = new twilio(accountSID, authToken);

client.verify.services(‘VA7**’)
.verifications
.create({to: ‘+64**’, channel: ‘sms’})
.then(verification => console.log(verification.status))
. catch (error => console.log(error));

}

thanks

Hey Shan/Team, more of a Twilio question I suppose. Where did you find the URLs for Twilio Verify API to be used from Wix’s fetch() calls.
My fetch() calls always return a 404 error.
My URL takes this form :-
https://verify.twilio.com/v2/Services --data-urlencode ‘FriendlyName=My First Verify Service’ -u ACxxxxxx:fdxxxxx
The same URL works fine when passed to curl.

Thanks