14

I am working on a angularfire project and I would like to know how can I create an user in Firebase 3 and once done, do not authenticate the specified user. In the previous Firebase version we had the method called createUser(email, password). Now we have the method createUserWithEmailAndPassword(email, password) only, it creates and authenticates the specified user.

benomatis
  • 5,536
  • 7
  • 36
  • 59
gcfabri
  • 564
  • 2
  • 7
  • 28
  • Can you give a quick explanation of why you do not want to authenticate? – theblindprophet Jun 09 '16 at 17:32
  • 1
    Sure. Imagine I am the admin and I want to create a new user for my system and set some permissions, edit some info, etc. Why I need that after user creation it be authenticated? The authentication should be made when user want to log in, right? Who creates the account is not the proper user.. – gcfabri Jun 09 '16 at 17:54
  • Possible duplicate of [Firebase kicks out current user](http://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user) – André Kool Jun 24 '16 at 12:50
  • Please take a look at [this answer](http://stackoverflow.com/a/38013551/4916627) – André Kool Jun 24 '16 at 12:51

2 Answers2

17

The answer to the question is: you can't.

We have similar situation where we have 'admin' users that can create other users. With 2.x this was a snap. With 3.x it's a fail as that capability was completely removed.

If you create a user in 3.x you authenticate as that user, and unauthenticate the account that's logged in.

This goes deeper as you would then need to re-authenticate to create another user; so the admin either does that manually or (cringe) stores the authentication data locally so it could be an automated process (cringe cringe, please don't do this)

Firebase has publicly stressed that 2.x will continue to be supported so you may just want to avoid 3.x.

Update:

one of the Firebaser's actually came up with a workaround on this. Conceptually you had an admin user logged in. You then create a second connection to firebase and authenticate with another user, that connection then creates the new user. Rinse - repeat.

Update again

See this question and answer

Firebase kicks out current user

Community
  • 1
  • 1
Jay
  • 34,438
  • 18
  • 52
  • 81
  • I must use the new console with firebase 2 or the previous one will be maintained? – gcfabri Jun 09 '16 at 18:39
  • The new console works fine with v2.x or v3.x of the API. You will want to continue to use the 2.x API to keep the functionality you need. – Jay Jun 09 '16 at 19:42
  • Thank you. I was thinking on a workaround to this situation. I will create a queue list created by a new method that we can call 'queueNewUser()', I can save the provided data to a new entry in database. On my login page, before firebase login process with method signInWithEmailAndPassword() we check if the email is on queue list. If yes, use the method createUserWithEmailAndPassword() and removes index from list. If not, use signInWithEmailAndPassword(). I can live with that, what you think? – gcfabri Jun 10 '16 at 13:08
  • I like the concept. However, by the default Rules in Firebase, you have to be authenticated to read and write. If you set .read to true, then it would work, but that would open up that node to anyone wanting to read it. – Jay Jun 10 '16 at 16:20
  • I thought to define key node with specified email. Even restricting read access only to current node key, comparing provided email address and the key? – gcfabri Jun 10 '16 at 18:55
  • @GabrielFabri That may wrap back around to your original issue where creating a user logs the current user out so you would then have to log back in to create another user. – Jay Jun 10 '16 at 19:30
  • Does the new pattern in 3.x seem... insane to anyone else? Who is this for? If you have admin users doing the management they now have several steps added for every single user they create. If you have a public sign up form you now can't wait to verify the user before signing them in. – James Proctor Jul 28 '16 at 19:20
  • @JamesProctor FB 3 has some... issues... IMO and a number of things don't appear to have been clearly thought through. See my updated answer with a link to another workaround for the issue. – Jay Jul 29 '16 at 18:17
  • Hi guys! Can somebody give advice on how to create that secondary connection described in the solution? Thanks in advance! – ZR87 Jul 19 '22 at 11:24
  • @ZR87 Some example code is in the link included in the question. Take a look at that. – Jay Jul 19 '22 at 17:09
6

You can do this using cloud function and firebase admin SDK. Create HTTP function like below.

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

admin.initializeApp(functions.config().firebase);

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.createUser = functions.https.onRequest((request, response) => {
    if (request.method !== "POST") {
        response.status(405).send("Method Not Allowed");
    } else {
        let body = request.body;

        const email = body.email;
        const password = body.password;
        const displayName = body.displayName;

        admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: password,
            displayName: displayName,
            disabled: false
        })
        .then((userRecord) => {
            return response.status(200).send("Successfully created new user: " +userRecord.uid);
        })
        .catch((error) => {
            return response.status(400).send("Failed to create user: " + error);
        });
    }
});

In your client app, call this function using Http request, for example using ajax

$.ajax({
       url: "the url generated by cloud function",
       type: "POST",
       data: {
           email: email,
           password: password,
           displayName: name
       },
       success: function(response) {
            console.log(response);
       },
       error: function(xhr, status, error) {
             let err = JSON.parse(xhr.responseText);
                 console.log(err.Message);
             }
       });
benomatis
  • 5,536
  • 7
  • 36
  • 59
elbert rivas
  • 1,464
  • 1
  • 17
  • 15
  • Is there any way to perform this task in Angular using ngZone which creates and authenticates the user outside the scope of the current running angular application? – Adnan Fayaz Sep 09 '21 at 11:15