将附加数据保存到 Firebase 认证。

huangapple go评论64阅读模式
英文:

Save additional data to firebase auth

问题

我已经在我的Flutter应用中创建了一个注册界面,该界面使用createUserWithEmailAndPassword()方法来创建并通过Firebase身份验证对用户进行身份验证。除了电子邮件和密码外,注册界面还包括一个用于用户姓名的字段。我想将姓名以及电子邮件和密码保存到Firebase身份验证中的用户帐户中。我应该如何做到这一点?

try {
    FirebaseAuth.instance
        .createUserWithEmailAndPassword(
            email: _emailTextController.text.trim(),
            password: _passwordTextController.text.trim()
            )
        .then((value) {
            print("Created New Account");
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => SignInScreen()));
        }).onError((error, stackTrace) {
            print("Error ${error.toString()}");
        });
} on FirebaseAuthException catch (e) {
    print(e);
    Utils.showSnackBar(e.message);
}
英文:

I've created a signup screen on my Flutter app that uses createUserWithEmailAndPassword() to create and authenticate users using Firebase Authentication. Along with email and password, the signup screen also includes a field for the user's name. I want to save the name as well as the email and password to the user's account in Firebase Authentication. How can I accomplish this?

try {
                        FirebaseAuth.instance
                            .createUserWithEmailAndPassword(
                                email: _emailTextController.text.trim(),
                                password: _passwordTextController.text.trim()
                                )
                            .then((value) {
                          print("Created New Account");
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => SignInScreen()));
                        }).onError((error, stackTrace) {
                          print("Error ${error.toString()}");
                        });
                      } on FirebaseAuthException catch (e) {
                        print(e);
                        Utils.showSnackBar(e.message);
                      }

答案1

得分: 2

有一些 firebase_auth 提供的方法允许你设置附加细节,例如使用 updateDisplayName 来保存他们的名字。

如果你需要存储关于用户更复杂的信息,你需要将用户的 ID 保存到数据库并从那里读取/写入信息。

所以类似于:

try {
    FirebaseAuth.instance
    .createUserWithEmailAndPassword(
        email: _emailTextController.text.trim(),
        password: _passwordTextController.text.trim()
    ).then((value) {
       value.user?.updateDisplayName("设置用户名"); // 更新他们的显示名称

       print("创建新帐户");
       Navigator.push(context, MaterialPageRoute(builder: (context) => SignInScreen()));
       }).onError((error, stackTrace) {
            print("错误 ${error.toString()}");
       });
   } on FirebaseAuthException catch (e) {
       print(e);
       Utils.showSnackBar(e.message);
   }
英文:

There are a few methods that firebase_auth provide that allow you to set additional details, you could for example use the updateDisplayName to save their name

将附加数据保存到 Firebase 认证。

If you need to store more complex information about the user, you will need to save the user's ID to a database and read/write information from there

So something like

try {
    FirebaseAuth.instance
    .createUserWithEmailAndPassword(
        email: _emailTextController.text.trim(),
        password: _passwordTextController.text.trim()
    ).then((value) {
       value.user?.updateDisplayName("SET USERNAME"); // Update their display name

       print("Created New Account");
       Navigator.push(context,MaterialPageRoute(                                builder: (context) => SignInScreen()));
       }).onError((error, stackTrace) {
            print("Error ${error.toString()}");
       });
   } on FirebaseAuthException catch (e) {
       print(e);
       Utils.showSnackBar(e.message);
   }

huangapple
  • 本文由 发表于 2023年4月17日 17:22:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定