Jawad
Jawad

Reputation: 308

Calling cloud function admin.auth().createUser() from android does not work

I am trying to call a Google cloud function from an Android app that does not work (First call just after deployment works 90 % of the times but subsequent calls fails, nothing is displayed on firebase log console either).

public Task<String> myCloudFunction() {

    return FirebaseFunctions.getInstance()
            .getHttpsCallable("createUser")
            .call(data)
            .continueWith(task -> {
                String result = (String) task.getResult().getData();
                return result;
            });

}

Endpoint in Functions Dashboard https://us-central1-xyz:555.cloudfunctions.net/createUser

This is how I call it.

public void callCloudFunction() {

    createFirebaseUserAccount.myCloudFunction()
            .addOnCompleteListener(new OnCompleteListener<String>() {
                @Override
                public void onComplete(@NonNull Task<String> task) {
                    if (!task.isSuccessful()) {
                        Exception e = task.getException();
                        if (e instanceof FirebaseFunctionsException) {
                            FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                            FirebaseFunctionsException.Code code = ffe.getCode();
                            Object details = ffe.getDetails();
                        } else {
                            Timber.d(task.getResult());
                        }
                    }
                }
            });
}

Here is the cloud function: $GOOGLE_APPLICATION_CREDENTIALS is pointing to service_key.json file which contains the private key.

admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: "https://XYZ.firebaseio.com"
});
exports.createUser = functions.https.onCall((data, context) => {
 const callerEmail = data.email;
 const callerPassword = data.password;
 const callerDisplayName = data.displayName;
 return admin.auth().createUser({ 
    email: callerEmail,
    emailVerified: false,
    password: callerPassword,
    displayName: callerDisplayName,
    disabled: false
   }).then(userRecord => {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully created new user:", userRecord.uid);
        return userRecord.uid;

    }).catch(error => {
        console.log("Error creating new user ", error);
        return error;
    });
  });

Thanks for reading! :)

Upvotes: 2

Views: 801

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

You're not returning a promise from the the function that contains the data to send to the client. Actually, you're not passing anything at all. You should instead return the promise chain from your async work:

return admin.auth().createUser({ 
    email: callerEmail,
    emailVerified: false,
    password: callerPassword,
    displayName: callerDisplayName,
    disabled: false
   }).then(userRecord => {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log("Successfully created new user:", userRecord.uid);
        return userRecord.uid;

    }).catch(error => {
        console.log("Error creating new user ", error);
        return error;
    });

Note the new return before the whole thing. You should definitely take some time to learn about how JavaScript promises work in order to make effective use of Cloud Functions, and they will not work correctly without observing their rules and conventions.

Upvotes: 2

Related Questions