Create user with firebase admin sdk that can signIn using email and password

12,469

There doesn't seem to be a reason to Stringify/Parse. This worked after I struggled with an unrelated typo...

FUNCTION CALL FROM REACT JS BUTTON CLICK

     <Button onClick={() => {
                    var data = {
                        "email": "[email protected]",
                        "emailVerified": true,
                        "phoneNumber": "+15551212",
                        "password": "randomPW",
                        "displayName": "User Name",
                        "disabled": false,
                        "sponsor": "Extra Payload #1 (optional)",
                        "study": "Extra Payload #2 (optional)"
                    };
                    var createUser = firebase.functions().httpsCallable('createUser');
                    createUser( data ).then(function (result) {
                        // Read result of the Cloud Function.
                        console.log(result.data)
                    });
                }}>Create User</Button>

And in the index.js in your /functions subdirectory:

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

// CREATE NEW USER IN FIREBASE BY FUNCTION
exports.createUser = functions.https.onCall(async (data, context) => {
  try {
    const user = await admin.auth().createUser({
      email: data.email,
      emailVerified: true,
      password: data.password,
      displayName: data.displayName,
      disabled: false,
    });
    return {
      response: user
    };
} catch (error) {
    throw new functions.https.HttpsError('failed to create a user');
  }
});

Screen shot of console output

Share:
12,469
Yasser Zubair
Author by

Yasser Zubair

Updated on June 22, 2022

Comments

  • Yasser Zubair
    Yasser Zubair about 2 years

    I'm using firebase admin SDK on cloud functions to create users using

      admin.auth().createUser({
    email: someEmail,
    password: somePassword,
    })
    

    now I want user to signIn using signInWithEmailAndPassword('someEmail', 'somePassword') but I cannot. I get the following error

    {code: "auth/user-not-found", message: "There is no user record corresponding to this identifier. The user may have been deleted."}