如何使用Firebase API创建新用户而不签出? [英] How create new user with Firebase API w/o signing out?

查看:145
本文介绍了如何使用Firebase API创建新用户而不签出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  firebase.auth()。createUserWithEmailAndPassword(email,密码)

成功创建新的Firebase用户。在调用之前,firebase.auth()。currentUser.uid 会在调用之后给出不同的uid。所以活跃的用户更改。

有没有办法让活跃的使用者建立另一位使用者,仍然是活跃使用者?

知道一个方法来解决这个问题



保存用户的电子邮件&密码到内存和创建用户重新登录回原来的用户,但它需要保存密码,我不喜欢它。

  firebase.auth()。createUserWithEmailAndPassword(email,password)
.then(user => {
//登录回主账户
firebase.auth()。signInWithEmailAndPassword(current_user_email (current_user_password).then(original_user => {

const userRef = firebase.database().ref()。child(`users / $ {user.uid}`);
userRef.set({
...


解决方案

要添加新的Firebase用户而不自动切换到该用户,您可以使用辅助Firebase实例来创建该用户。例如:

  var firebaseConfig = {
apiKey:asdasdlkasjdlkadsj,
authDomain:asdfg-12345.firebaseapp.com,
databaseURL:https://asdfg-12345.firebaseio.com ,
storageBucket:asdfg- 12345.appspot.com,
};

firebase.initializeApp(firebaseConfig); // Intialisefirebase
var fbAdmin = firebase.initializeApp(firebaseConfig,Secondary); // IntialisefbAdmin作为名为Secondary的实例。

创建用户:

<$ p $ (函数(newUser)){
//成功。
//注销$($)。
.then b $ b fbAdmin.auth()。signOut()
.then(function(){
//注销成功
console.log('fbAdmin signed out。');
},函数(错误){
//发生错误
console.log('fbAdmin。'错误信号);
console.log(error);
});
},function(error){
//这里有个问题
console.log(error.code);
console.log error.message);
})

以上是纯JavaScript代码。如果您使用 auto配置来管理多个环境。 (另请参阅保留的网址)。



如果您使用的是node.js,则可以使用 Firebase管理Node.js SDK 。有关需求和设置信息,请参阅安装说明



编辑:这个答案


user signs in. I make a call to create new user with

firebase.auth().createUserWithEmailAndPassword(email, password)

it successfully creates new firebase user. Before this call firebase.auth().currentUser.uid gives different uid then after the call. So active user changes.

Is there any way for active user create another user and still be active user?

I know one way to work this around:

Save user's email & password to memory and after creating user re-login back to original user, but it requires saving password and i don't like it.

firebase.auth().createUserWithEmailAndPassword(email, password)
      .then(user => {
        // login back to main account
        firebase.auth().signInWithEmailAndPassword(current_user_email, current_user_password).then(original_user => {

          const userRef = firebase.database().ref().child(`users/${user.uid}`);
          userRef.set({
            ...

解决方案

To add a new Firebase user without automatically switching to that user you can use a secondary Firebase instance to create the user. For example:

var firebaseConfig = {
  apiKey: "asdasdlkasjdlkadsj",
  authDomain: "asdfg-12345.firebaseapp.com",
  databaseURL: "https://asdfg-12345.firebaseio.com",
  storageBucket: "asdfg-12345.appspot.com",
};

firebase.initializeApp(firebaseConfig);  // Intialise "firebase"
var fbAdmin = firebase.initializeApp(firebaseConfig, "Secondary"); // Intialise "fbAdmin" as instance named "Secondary".

To create the user:

fbAdmin.auth().createUserWithEmailAndPassword(email, password)
 .then(function(newUser) {
  // Success. 
  // Log out.
  fbAdmin.auth().signOut()
   .then(function () {
    // Sign-out successful.
    console.log('fbAdmin signed out.');
  }, function (error) {
    // An error happened.
    console.log('Error siging out of fbAdmin.');
    console.log(error);
  });
}, function (error) {
  // There's a problem here.
  console.log(error.code);
  console.log(error.message);
})

The above is for plain JavaScript. It's a bit trickier if you're using auto configuration to manage multiple environments. (See also Reserved URLs).

If you're using node.js you can use the Firebase Admin Node.js SDK. For requirements and set up information see the installation instructions.

Edit: Pretty much identical to this answer.

这篇关于如何使用Firebase API创建新用户而不签出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆