是否可以使用电子邮件和密码创建用户并立即添加用户名(显示名称)?

问题描述 投票:0回答:2

当您在 flutter 上使用 firebase auth 创建用户时,您可以在创建用户时设置电子邮件和密码。我还需要在创建用户时设置显示名称,但我发现只有在创建用户后才能设置显示名称。

我用过这样的代码

FirebaseAuth.instance.
    createUserWithEmailAndPassword(
  email: eMail.text.trim(),
  password: passWord.text.trim(),
)
    .then((uC){
  uC.user!
      .updateDisplayName(userName.text.trim());
}

但这不是我需要的

flutter firebase-authentication
2个回答
0
投票

是的,您可以在注册成功后使用 fireStore 将用户详细信息添加到 firebase。

Future<bool> signUp(String name,String email,String phone,String password,BuildContext context) async {
    try{

      //loading dialog starts
      showLoadingDialog(context);
      UserCredential credential = await _auth.createUserWithEmailAndPassword(email: email, password: password);

      //if user creation is success, create a user model
      if(credential.user == null) return false;
      UserModel user = UserModel(
        uId: credential.user!.uid.toString(),
        name: name,
        email: credential.user!.email.toString(),
        phone: phone,
      );

      //and upload user model to firebase
      await _fireStore.collection('users').doc(credential.user!.uid).set(user.toMap()).then((value){
        showSnackBar(context, "Sign Up Successful!");
      });
      return true;
    }on FirebaseAuthException catch(exception){
      showSnackBar(context, getMessageFromErrorCode(exception.code));
      return false;
    }finally{
      //pops loading dialog
      Navigator.pop(context);
    }
  }

0
投票

使用 Flutter(和其他客户端 SDK),它总是需要额外的 API 调用来设置用户的显示名称。

如果您的应用需要自动创建用户并设置其显示名称,请考虑在受信任的环境(例如您的开发机器、您控制的服务器或 Cloud Functions/Cloud Run)中使用 Admin SDK 来执行该操作原子操作,为你的这个创建一个端点,然后从你的应用程序中调用它。

另见:

© www.soinside.com 2019 - 2024. All rights reserved.