8

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?

2
  • 1
    I don't understand what you're trying to do. Can you show some code for illustration? Commented Nov 13, 2017 at 16:05
  • I edited the question, hopefully it is better now. Commented Nov 13, 2017 at 17:14

1 Answer 1

16

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: "[email protected]",
  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);
  });
Sign up to request clarification or add additional context in comments.

5 Comments

Can I use this for login too?
Cloud Functions normally access Firebase through the Admin SDK, so they don't need to authenticate.
Sure, but in my case, I want to create a login while using a backend system. so the user can only enter an email and password, everything else should be handled server side. I don't see an option to do that here, unfortunately.
@J.Doe you can create your own auth provider, and inject whatever logic you like including hitting your cloud functions...firebase.google.com/docs/auth/web/custom-auth
Don't you have to return the promise? In three places. Line 1, end of then and end of catch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.