如何在这个颤振中添加电子邮件验证?我需要在登录之前验证用户

Posted

技术标签:

【中文标题】如何在这个颤振中添加电子邮件验证?我需要在登录之前验证用户【英文标题】:How to add email verification in this flutter? I need to verify users before signing them in 【发布时间】:2021-08-05 12:03:42 【问题描述】:

此代码必须在用户点击登录时为我提供电子邮件验证,他们必须先验证他们的邮件,然后才能登录

            onPressed: () async

                      if (nameTextEditingController.text.length < 4) 
                         displayToastMessage(
                             "Username Must be atleast 4 characters",
                             context);
                       else if (!emailTextEditingController.text
                          .contains("@")) 
                        displayToastMessage("Email is not Valid ", context);
                       else if ((phoneTextEditingController.text.length <10 )) 
                        displayToastMessage(
                            "Invalid Phone number  ", context);
                       else if (passwordTextEditingController.text.length <
                          7) 
                        displayToastMessage(
                            "Password must be 6 characters long", context);
                       else 
                        registerNewUser(context);
                      





                    ),
              ],
            ),
          ),
          FlatButton(
              onPressed: () 

                Navigator.pushNamedAndRemoveUntil(
                    context, LoginScreen.idscreen, (route) => false);
              ,
              child: Text("Already have an Account ? Login Here",style: TextStyle(fontSize: 17,color: Colors.white),)),
        ],
      ),
    ),
  ),
);


     final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

     void registerNewUser(BuildContext context) async 




showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context)
    
      return ProgressDialog(message: "Signing In , please wait....",);
    );



final User firebaseUser = (await _firebaseAuth
        .createUserWithEmailAndPassword(
            email: emailTextEditingController.text,
            password: passwordTextEditingController.text)
        .catchError((errMsg)
          Navigator.pop(context);
          displayToastMessage("Error:"+errMsg.toString(), context);



)
)
    .user;

if (firebaseUser != null) //user has been created

  Map userDataMap = 
    "name": nameTextEditingController.text.trim(),
    "email": emailTextEditingController.text.trim(),
    "phone": phoneTextEditingController.text.trim(),
  ;

  usersRef.child(firebaseUser.uid).set(userDataMap);
  displayToastMessage("Welcome to Yatra. Book ride as you like", context);

  Navigator.pushNamedAndRemoveUntil(
       context, MainScreen.idscreen, (route) => false);


     else 
  Navigator.pop(context);
  //if error display messages
  displayToastMessage("UserAccount hasn't been Created", context);

  
    


       

       displayToastMessage(String message, BuildContext context)
         
      Fluttertoast.showToast(msg: message);
        

我遇到了 sendEmailVerification(); firebase_auth 包提供的方法,但需要一些关于设置它的建议。有没有人可以效仿的工作代码示例?

【问题讨论】:

【参考方案1】:

您可以分两步实现:

    创建用户后发送验证邮件 检查用户是否已通过验证,并决定在用户​​尝试登录时显示什么 UI

发送验证邮件:

     void registerNewUser(BuildContext context) async 

       ... //Your existing code for creating the new user

       if (firebaseUser != null)  
         Map userDataMap = 
           "name": nameTextEditingController.text.trim(),
           "email": emailTextEditingController.text.trim(),
           "phone": phoneTextEditingController.text.trim(),
       ;

       usersRef.child(firebaseUser.uid).set(userDataMap);

       await firebaseUser.sendEmailVerification();

       await handleUserEmailVerification(context, user: firebaseUser);

       
       ... //Rest of your code
     


检查用户是否通过验证

      Future<void> handleUserEmailVerification(BuildContext context, @required User user) async 
        if (user.isEmailVerified) 
          // Navigate the user into the app
         else 
          // Tell the user to go verify their mail
        
     

您也可以使用handleUserEmailVerification这个方法登录。

让用户登录

Future<void> signInUser(BuildContext context) async 
  final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  User firebaseUser = await firebaseAuth
        .signInWithEmailAndPassword(
          email: loginEmailController.text,
          password: loginPasswordController.text,
  );

  await handleUserEmailVerification(context, user: firebaseUser);


【讨论】:

收到错误未定义名称“上下文”。 for await handleUserEmailVerification(context, user: firebaseUser); @SandeepKshetri,我通过将BuildContext 传递给signInUser 方法更新了答案。

以上是关于如何在这个颤振中添加电子邮件验证?我需要在登录之前验证用户的主要内容,如果未能解决你的问题,请参考以下文章

如何在颤振应用程序中使用相同的 Firebase 匿名用户

颤振 |如何将数据添加到 Firestore 中的现有文档

如何在进入主页之前验证电子邮件

使用 SwiftUI、Combine 和 Firebase,我如何在将用户的帐户链接到电子邮件/密码之前验证用户是不是已匿名登录?

试过用验证码登录NAS吗?

在firebase中实现电话和电子邮件身份验证的最佳方法是一次性使用?