node.jstypescripthttprequestandroid-json

How to get data from Request sent through HTTP callable function?


I am using the Firebase Admin SDK to complete administrative functions. The functions i have deployed are on the Firebase Cloud Function in my console. I have been able to call these functions from the application however i do not know to get the values from the mapped data i sent through the call. If i'm correct that should be able to be retrieved through the request variable but when i try something like request.email for example, this does not seem to be a valid argument.

MainActivity.java

public Task<String> callCloudFunction() {
            FirebaseFunctions mFunctions = FirebaseFunctions.getInstance();
            Map<String, Object> data = new HashMap<>();
            data.put("email", "new@example.com");
            data.put("password", "changePassword");
            data.put("displayName", "Mike Example");
            data.put("trainerId", "Mike Example");
            data.put("photoURL", "NULL");

            return mFunctions
                    .getHttpsCallable("mkUser")
                    .call(data)
                    .continueWith(new Continuation<HttpsCallableResult, String>() {
                        @Override
                        public String then(@NonNull Task<HttpsCallableResult> task) {
                            // This continuation runs on either success or failure, but if the task
                            // has failed then getResult() will throw an Exception which will be
                            // propagated down.
                            return (String) task.getResult().getData();
                        }
                    });

    }

index.ts

export const mkUser = functions.https.onRequest((request, response) =>{

  admin.auth().createUser({
      email: request.email, //IS NOT A VALID ARGUMENT ('email' CANNOT BE FOUND)
      emailVerified: false,
      password: 'secretPassword',
      displayName: 'John Doe', //VALUES ARE HARDCODED BUT I WOULD LIKE TO USE 'Mike Example' SENT FROM MAIN ACTIVITY
      photoURL: 'http://www.example.com/12345678/photo.png',
      disabled: false
    })
      .then(function(userRecord: { uid: any; }) {
        // See the UserRecord reference doc for the contents of userRecord.
        console.log('Successfully created new user:', userRecord.uid);
      })
      .catch(function(error: any) {
        console.log('Error creating new user:', error);
      });
});

Solution

  • So I've figured out the problem i encountered and how to resolve the issue.

    Firstly, inside the index.ts, the HTTP function I had used was .onRequest but rather if I had changed that to .onCall then the code that i have used to call the HTTP function inside MainActivity.java would work.

    Using .onCall changes the received information from 'request' to 'data'; this can then be used to access the mapped information sent through the call within the application.

    index.ts

    export const mkUser = functions.https.onCall(async (data, context) =>{
      try {
        const userRecord = await admin.auth().createUser({
          email: data.email,
          emailVerified: false,
          password: data.password,
          displayName: data.displayName,
          disabled: false
        })
        console.log('Successfully created new user:', userRecord.uid);
       } catch (error) {
        console.log('Error creating new user:', error);
        return "ERROR"
       }
    });