Create users server-side Firebase Functions

Viewed 5381

I am trying to create a login GET request to the server side using the Firebase cloud functions.

Is it possible to auth using firebase functions? I tried npm i firebase inside the functions folder, but it fails to work.

Is there an option to create users using server-side code?

1 Answers

To create Firebase Authentication users from within Cloud Functions you use the Firebase Admin SDK for Node.js.

To install it, follow the instructions in the documentation. Mostly it's:

$ npm install firebase-admin --save

And then import it into your index.js using:

var admin = require("firebase-admin");

To create a user follow the instructions in this documentation:

admin.auth().createUser({
  email: "user@example.com",
  emailVerified: false,
  phoneNumber: "+11234567890",
  password: "secretPassword",
  displayName: "John Doe",
  photoURL: "http://www.example.com/12345678/photo.png",
  disabled: false
})
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log("Successfully created new user:", userRecord.uid);
  })
  .catch(function(error) {
    console.log("Error creating new user:", error);
  });
Related