简体   繁体   English

如何在这个 flutter 中添加 email 验证? 我需要在登录之前验证用户

[英]How to add email verification in this flutter? I need to verify users before signing them in

This code must provide me email verification for the user when they click sign in firstly they have to verify their mail afterwards they can sign in此代码必须在用户单击登录时为我提供 email 验证首先他们必须验证他们的邮件然后才能登录

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

I came across the sendEmailVerification();我遇到了 sendEmailVerification(); method provided by firebase_auth package, but need some advice on setting it up. firebase_auth package 提供的方法,但需要一些关于设置的建议。 Does anyone have a working code example to follow?有没有人可以遵循的工作代码示例?

You can implement this in two steps:您可以分两步实现:

  1. Send the verification mail after creating the user创建用户后发送验证邮件
  2. Check if the user is verified and decide what UI to show when the user tries to login in检查用户是否已通过验证,并决定在用户尝试登录时显示什么 UI

Sending verification mail:发送验证邮件:

     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
     }


Checking if the user is verified检查用户是否通过验证

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

You can also use this handleUserEmailVerification method for sign-in.您还可以使用此handleUserEmailVerification方法进行登录。

Signing a user in登录用户

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在授予他们访问应用程序的权限之前,如何在 Flutter 和 Firebase 中验证新用户的电子邮件? - how do I verify a new user's email in Flutter and Firebase before granting them access to the app? 使用FirebaseUI-auth时,如何在无需再次登录的情况下验证电子邮件? - How to verify email without the need of signing in again while using FirebaseUI-auth? 我想在不使用 Firebase 身份验证的情况下向用户电子邮件发送验证链接 [Flutter] - I want to send a verification link to users email without using firebase authentication [Flutter] FLutter FirebaseAuth Email 验证 - FLutter FirebaseAuth Email verification 如何在解析服务器中添加电子邮件验证 - How to add email verification in a parse server 如何在Flutter中的列表之前和/或之后添加小部件 - how do I add widgets before and/or after a list in flutter parse.com-如何在电子邮件中生成和添加验证URL? - parse.com - How to generate and add verification url within email? Firebase阻止在电子邮件验证之前创建帐户 - Firebase Prevent Creating Account Before Email Verification 如何自动进行电话验证(需要发送到手机的验证码)? - How do I automate Phone verification (need codes sent to mobile)? 如何使用 Firebase 发送验证电子邮件? - How to send verification email with Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM