Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase + Flutter - Cloud functions onCall result in "unauthenticated" error from Android app

I have deployed the following test https.onCall function to Cloud Functions on firebase - deployed using node 10:

export const helloWorld = functions.https.onCall((data, context) => {

    return {
        "message": "Hello, world",
    }

}); 

This function returns as expected when tested from a node environment.

However, within my flutter (android) app - using the Cloud functions plugin for Flutter, I'm getting the following authentication error, despite being logged in (via phone number auth):

Flutter code:

void _checkAuth() async { 

    print("Check auth");
    final FirebaseAuth _auth = FirebaseAuth.instance;
    var user = await _auth.currentUser();

    print(user.toString());

    _testFunCall();
}

void _testFunCall() async {
    HttpsCallable callable = CloudFunctions.instance
        .getHttpsCallable(functionName: 'helloWorld');

    try {
        final HttpsCallableResult result = await callable.call();
        print(result.data);

    } on CloudFunctionsException catch (e) {
        print('caught firebase functions exception');
        print(e.code);
        print(e.message);
        print(e.details);
    } catch (e) {
        print('caught generic exception');
        print(e);
    }
}

Error:

I/flutter ( 4662): caught firebase functions exception
I/flutter ( 4662): UNAUTHENTICATED
I/flutter ( 4662): Unauthenticated
I/flutter ( 4662): null

Any ideas?

like image 665
dsg38 Avatar asked May 13 '19 21:05

dsg38


1 Answers

The problem was using Node 10 when deploying to cloud functions.

Node 10 is currently in beta. Switched down to node 8 and it works fine:

In package.json in your cloud functions dir, switch:

  "engines": {
    "node": "10"
  },

to:

  "engines": {
    "node": "8"
  },
like image 60
dsg38 Avatar answered Oct 19 '22 14:10

dsg38