如何在不登录Firebase / js的情况下注册用户

时间:2019-12-27 10:44:00

标签: javascript firebase firebase-authentication

我正在管理员网站上工作,实际上我需要知道如何在不丢失管理员注册的情况下对用户进行注册,因为我需要仅管理员才能创建用户帐户。 我正在使用Firebase电子邮件/密码验证。

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;
    // ...
  });
});

但是创建帐户后,它将自动使用该新帐户登录。

1 个答案:

答案 0 :(得分:3)

您可以初始化一个单独的Firebase SDK实例来处理您所有的帐户创建请求。

以最简单的形式,您可以使用:

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;
    // ...
});

如果遇到诸如 Firebase应用程序'auth-worker'已经初始化之类的错误,则可以将其包装在安全的吸气剂中,以免发生此类错误:

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');