4

我有以下代码使用flutter登录firebase。这工作正常。

FirebaseAuth.instance.signInWithEmailAndPassword(email: myemail, password: mypassword);

成功验证后,我正在使用以下代码创建 nw 用户。

UserCredential userCredentials = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: myemail, password: mypassword);

但是,随着方法,当前用户会发生变化。我想在不更改当前用户的情况下创建用户。这可能吗?

4

3 回答 3

2

我找到了一种解决方案,它对我有用

在像这样的主 dart 文件中 initializeApp

void main() async{
await Firebase.initializeApp();
  runApp(MyApp());
}

创建一个简单/管理员用户

FirebaseAuth.instance.signInWithEmailAndPassword(email: myemail, password: mypassword);

添加其他用户

FirebaseApp app = await Firebase.initializeApp(
        name: 'secondary', options: Firebase.app().options);
await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(
            email: _email, password: _password);

app.delete();
于 2021-02-12T05:08:14.127 回答
1

这是Flutter: Firebase Authentication Create User without Automatically Logging In的副本,但我不能标记这个问题,因为它有一个开放的赏金。

您需要创建 FirebaseApp 的第二个实例并使用该实例创建新用户:

static Future<UserCredential> register(String email, String password) async {
    FirebaseApp app = await Firebase.initializeApp(
        name: 'Secondary', options: Firebase.app().options);
    try {
        UserCredential userCredential = await FirebaseAuth.instanceFor(app: app)
        .createUserWithEmailAndPassword(email: email, password: password);
    }
    on FirebaseAuthException catch (e) {
      // Do something with exception. This try/catch is here to make sure 
      // that even if the user creation fails, app.delete() runs, if is not, 
      // next time Firebase.initializeApp() will fail as the previous one was
      // not deleted.
    }
    
    await app.delete();
    return Future.sync(() => userCredential);
}
于 2021-02-16T02:11:32.243 回答
0

您尝试做的事情并不安全(生成其他人的密码)而且您在这个意义上是正确的,作为管理员,您无法为另一封电子邮件创建密码。

你想做的是sendSignInLinkTOEmail

这与注册 GSuit 和其他流行服务的程序相同。

如果您确实需要设置用户,则必须使用单独的身份验证模块(应该很容易编写自己的)而不是 firebase,然后在用户注册时将其链接在一起。这是一种hacky方式,我不推荐它。

于 2021-02-17T22:26:41.330 回答