1
votes

I'm creating users using the admin SDK and I'm wanting them to be able to login with email and password. For some reason when I create users through the client using only email and password, the user can login using those credentials, but when I create a user using the admin SDK, the user is shown as anonymous in the auth dashboard, and the user can't login using their email and password. No errors are shown client side or Firebase side.

How can I create a Firebase user using the admin SDK and have that user linked to email authentication?

Node:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

  exports.createUser = functions.https.onRequest(async (req, res) => {
    //grab the email and password parameters
    await admin.auth().createUser({
        email: req.query.email,
        password: req.query.password
      })
      //create the user
      .then(function(userRecord) {        
        const child = userRecord.uid;
        console.log('Successfully created new user:', userRecord.uid);
        res.json({
            status: 201,
            data: {
                "message": userRecord.uid
            }
        });
      })
      //handle errors
      .catch(function(error) {
        console.log();
        res.json({
            status: 500,
            data: {
                "error": 'error creating user: ', error
            }
        });
      });
  });

Swift:

func createChild(for parent: Parent,
                     with firstName: String,
                     lastName: String,
                     displayName: String?,
                     chores: [Chore]?,
                     username: String,
                     password: String,
                     completion: @escaping () -> Void = { }) {
        let funcCallDict = [
            "email": username,
            "password": password
        ]
        functions.httpsCallable(addChildIdentifier).call(funcCallDict) { (result, error) in
            if let error = error {
                NSLog("error: adding child with firebase function: \(error)")
                completion()
                return
            }
        }
        completion()
}

Firebase Function Log

Auth Console

1

1 Answers

2
votes

Your function is an HTTP type trigger:

exports.createUser = functions.https.onRequest

But you're trying to invoke it as a callable type trigger:

functions.httpsCallable(addChildIdentifier).call(funcCallDict)

(Note that a callable trigger would be defined with onCall, not onRequest.)

As you can see from the documentation links, they are not the same thing. You are probably invoking the HTTP trigger, and it's not actually getting the arguments you expect from the client, since the protocol is different between them. Try logging req.query.email in the function to see what I mean.

You will have to either make your function a proper callable so it can be invoked from the client using the provided library, or change the way you invoke it on the client to use a regular http library instead of the Firebase library.