Flutter:Firebase身份验证无需自动登录即可创建用户 [英] Flutter: Firebase Authentication Create User Without Automatically Logging In

查看:51
本文介绍了Flutter:Firebase身份验证无需自动登录即可创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的flutter应用程序中具有使用Firebase身份验证的用户管理功能.我可以使用 firebase_auth createUserWithEmailAndPassword()函数注册新的用户帐户.

I have a user management feature in my flutter app that uses firebase authentication. I can register new user accounts using firebase_auth's createUserWithEmailAndPassword() function.

return await FirebaseAuth.instance.
    createUserWithEmailAndPassword(email: email, password: password);

问题是注册成功后,即使我已经登录,它也会自动将我的 FirebaseAuth 实例认证为新用户.

The problem is when the registration is successful it automatically authenticates my FirebaseAuth instance as the new user even though I am already logged in.

我遇到了以下答案: Firebase淘汰了当前用户 ,但这是在javascript和api稍有不同.

I came across this answer: Firebase kicks out current user but it's in javascript and has a slightly different api.

我该如何在飞镖上做同样的事情?

How can I do the equivalent in dart?

推荐答案

已更新: firebase_core ^ 0.5.0 firebase_auth ^ 0.18.0 + 1 已弃用了adadion的 answer 中共享的一些旧类.

Updated: firebase_core ^0.5.0 and firebase_auth ^0.18.0+1 has deprecated some of the old classes as shared in adadion's answer.

以下是为 firebase_core ^ 0.5.1 firebase_auth ^ 0.18.2 更新的代码.

Below is code updated for firebase_core ^0.5.1 and firebase_auth ^0.18.2.

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


原始答案

我试用了Firebase身份验证api,目前可以使用的解决方案是:

I experimented with the firebase authentication api and my current working solution is:

// Deprecated as of `firebase_core ^0.5.0` and `firebase_auth ^0.18.0`.
// Use code above instead.

static Future<FirebaseUser> register(String email, String password) async {
    FirebaseApp app = await FirebaseApp.configure(
        name: 'Secondary', options: await FirebaseApp.instance.options);
    return FirebaseAuth.fromApp(app)
        .createUserWithEmailAndPassword(email: email, password: password);
}

从本质上讲,这归结于创建 FirebaseAuth 的新实例,因此从 createUserWithEmailAndPassword()进行的自动登录不会影响默认实例.

Essentially it comes down to creating a new instance of FirebaseAuth so the automatic login from createUserWithEmailAndPassword() do not affect the default instance.

这篇关于Flutter:Firebase身份验证无需自动登录即可创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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