0
votes

I am trying to get the errors when creating a user with Firebase Cloudfunctions and the Admin SDK with nodeJS.

When creating the user everything works correctly since the created user returns me but when creating an error I do not get what error it is in the catch of the promise.

So in the catch of the frontend side never receive anything everything receives it as if it were correct

Example: I do not enter the email to create the new user and the request is a status 200 but the answer is:

{"result":{"errorInfo":{"code":"auth/invalid-password","message":"The password must be a string with at least 6 characters."},"codePrefix":"auth"}}

This is my code for the function:

exports.addNewUser = functions.https.onCall((data, context) => {
    return admin.auth().createUser({
      email: data.email,
      emailVerified: true,
      password: data.password,
      displayName: data.name,
      disabled: false
    }).then(userRecord => {
        console.log('Successfully created new user:', userRecord.uid);
        return userRecord;
      })
      .catch( error => {
        console.log('Error creating new user:', error);
        return error;
      });
  });

This works fine when creating the user but the error does not return to me in case it exists.

Something I did is that instead of making a return of the error only return a string and that if it returns it correctly.

1
I think you create some rules in database regarding password...Shantanu Sharma
no friend at all, there are only rules for the administrator to create new usersOscarDev

1 Answers

2
votes

This is because to handle errors in a Callable Cloud Function you need to throw a functions.https.HttpsError, as explained in the documentation.

So the following will work:

exports.addNewUser = functions.https.onCall((data, context) => {
    return admin.auth().createUser({
        email: data.email,
        emailVerified: true,
        password: data.password,
        displayName: "dataname",
        disabled: false
    }).then(userRecord => {
        console.log('Successfully created new user:', userRecord.uid);
        return userRecord;
    })
    .catch(error => {
        console.log('Error creating new user:', error);
        throw new functions.https.HttpsError('invalid-argument', error.message);
    });
});

Note also the section in the doc about how to handle errors on the client.