1

I'm working on administrator web site, and Actually I need to know how to sign up users without losing my admin sign up, Cause I need to make only admin can create users accounts. I'm using firebase Email/Pw authentification.

const CreateCh = document.querySelector('#CreateChaufeurs');

CreateCh.addEventListener('submit',(e)=>{
    e.preventDefault();

//get chaufeur info
const email = CreateCh['exampleEmail11'].value;
const password = CreateCh['examplePassword11'].value;
const Fname = CreateCh['Fname'].value;
const Address = CreateCh['exampleAddress'].value;
const Tel = CreateCh['exampleAddress2'].value;
const Ville = CreateCh['exampleCity'].value;
const Etat = CreateCh['exampleState'].value;
const Cp = CreateCh['exampleZip'].value;
const AGE = CreateCh['AGE'].value;
console.log(password, email, Fname,AGE,Address,Tel,Ville,Etat,Cp );
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
  });
});

but after the creation of the account, it automatically logs in with that new account.

1 Answers1

4

You can initialize a separate Firebase SDK instance to handle all of your account creation requests.

In it's simplest form, you can use:

let authWorkerApp = firebase.initializeApp(firebase.app().options, 'auth-worker');
let authWorkerAuth = firebase.auth(authWorkerApp);
authWorkerAuth.setPersistence(firebase.auth.Auth.Persistence.NONE); // disables caching of account credentials

authWorkerAuth.createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    // ...
});

If you encounter errors such as Firebase app 'auth-worker' is already initialised, you can wrap it in a safe getter to avoid such an error:

function getFirebaseApp(name, config) {
    let foundApp = firebase.apps.find(app => app.name === name);
    return foundApp ? foundApp : firebase.initializeApp(config || firebase.app().options, 'auth-worker');
}

let authWorkerApp = getFirebaseApp('auth-worker');
samthecodingman
  • 23,122
  • 4
  • 30
  • 54