0

Im trying to implement the authy-node phone verification with firebase functions and my app in react-native the message is sent to the correct mobile phone but for some reason the data I get back from the api is null any ideas out there

My Api firebase functions

import * as functions from 'firebase-functions';
const authy = require('authy')('mySecret');


export const getCode = functions.https.onCall((data, context) => {
  const {
     number, countryCode
  } = data;

 return authy.phones().verification_start(number, countryCode, { via: 
'sms', locale: 'en', code_length: '4' }, (err: any, res: any) => {
    if (err) {
        throw new functions.https.HttpsError(err);
    }
    return res;
});
});

and this is my call from my app

export default class test extends Component {

constructor() {
 super();
}

 componentWillMount() {
 const getCode = firebase.functions().httpsCallable('getCode');
 getCode({number: 'theCorrectNumber', countryCode: '44'})
   .then(function (result) {
     const data = result;
     console.log(data)
   }).catch( function (error){
   console.log(error)
 })
}

render() {
 return (
  <View/>
 );
}
}
J.lan
  • 223
  • 4
  • 17

1 Answers1

0

Twilio developer evangelist here.

From what I can see in the Authy Node library that I'm assuming you're using, making a request to the API does not return a Promise. Instead it is built with request and responds to asynchronous requests using callbacks only. You do deal with the callback, but you are returning the result of calling the asynchronous function, which is null, rather than the result from the callback.

Perhaps including a callback as part of the function call would work better:

import * as functions from 'firebase-functions';
const authy = require('authy')('mySecret');

export const getCode = functions.https.onCall((data, callback) => {
  const { number, countryCode } = data;

  return authy
    .phones()
    .verification_start(
      number,
      countryCode,
      { via: 'sms', locale: 'en', code_length: '4' },
      callback
    );
});

You can then use it like this:

export default class test extends Component {
  constructor() {
    super();
  }

  componentWillMount() {
    const getCode = firebase.functions().httpsCallable('getCode');
    getCode({ number: 'theCorrectNumber', countryCode: '44' }, (err, res) => {
      if (err) {
        throw new functions.https.HttpsError(err);
      }
      const data = res;
      console.log(data);
    });
  }

  render() {
    return <View />;
  }
}

Let me know if that helps at all.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hi, Thanks for your response. Yes I am using the authy node sorry for not motioning. As for what you provided I still get an sms on my device but now the console does not log anything not even an error or the 'res' any suggestions? – J.lan Jan 24 '19 at 22:19
  • I'm not familiar with Firebase and using `functions.https.onCall` to define functions I'm afraid, so some of this was a bit of a guess. As you can see, I updated the function we defined to take a callback function and return to it, perhaps that's not possible with Firebase? I can see some documentation that suggests using promises to keep functions alive while they a processing asynchronously. Do you know any more about that? – philnash Jan 24 '19 at 22:25
  • yes I just added to your code the async and await and even putting a variable to equal all the function results in getting an Object with null again – J.lan Jan 24 '19 at 22:33
  • `async componentWillMount () { const getCode = firebase.functions().httpsCallable('getCode'); const s = await getCode({ number: 'myNumber', countryCode: '44' }, (err, res) => { if (err) { throw new functions.https.HttpsError(err); } const data = res; console.log(res); }); console.log(s) }` – J.lan Jan 24 '19 at 22:34
  • without await s comes back with a promise – J.lan Jan 24 '19 at 22:42
  • Ah, but nothing within `getCode` returns a promise, which is why it is `null`. – philnash Jan 24 '19 at 22:42
  • If Firebase is going to require promises, you might need to rewrite [the verification call](https://github.com/evilpacket/node-authy/blob/master/index.js#L110-L131) using something like [request-promise](https://www.npmjs.com/package/request-promise). – philnash Jan 24 '19 at 22:43
  • ok, i'll look into it, I honestly do not no much about the subject. Thanks for your help – J.lan Jan 24 '19 at 22:45