最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - How can I make sure the inputted email is a real one - Stack Overflow

programmeradmin1浏览0评论

I am new to flutter and dart, but have some fundamentals of programing. When I enter a random email with a random password, Firebase will create a user with that email and password via createUserWithEmailAndPassword method without making sure if that email is a real one. How can I make sure the email is real?

Here is what I did:

void initState() {
  late final _email = TextEditingController();
  late final _firstName = TextEditingController();
  late final _familyName = TextEditingController();
  late final _password = TextEditingController();
  super.initState();
}

@override
  void dispose() {
    _email.dispose();
    _firstName.dispose();
    _familyName.dispose();
    _password.dispose();
    super.dispose();
  }

after initstate and dispose , I created a class extends StatefulWidget, which had inside it 4 TextFields with 4 controllers and a button which the onpressed function is :

onPressed: () async {
    final email = _email.text.trim();
    final password = _password.text.trim();
    try {
      FirebaseFirestore.instance;
      final usercredential =
          await FirebaseAuth.instance
              .createUserWithEmailAndPassword(
                  email: email,
                  password: password);
      final userEmail =
          usercredential.user?.email;
      print(usercredential);
      if (userEmail == email) {
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(
            builder: (context) =>
                UsernameAndPhonenumber(),
          ),
          (route) => route.isFirst,
        );
      } else {
        setState(() {
          msg = "Please verify your email";
        });
      }
    } on FirebaseAuthException catch (e) {
      if (e.code == "channel-error") {
        setState(() {
          msg = "Can't be empty";
        });
      } else if (e.code ==
          "weak-password") {
        print("weak password");
        setState(() {
          msg =
              "The password provided is too weak.";
        });
      } else if (e.code ==
          "email-already-in-use") {
        print(
            "The account already exists for that email.");
        setState(() {
          msg =
              "The account already exists for that email.";
        });
      } else if (e.code ==
          "invalid-email") {
        print("invalid email");
        setState(() {
          msg = "invalid email";
        });
      } else {
        print("SOME ERROR HAPPENED HERE");
        print(e.code);
        print(e.code);
        setState(() {
          msg = "unknown erorr occured";
        });
      }
    }
  },

I am new to flutter and dart, but have some fundamentals of programing. When I enter a random email with a random password, Firebase will create a user with that email and password via createUserWithEmailAndPassword method without making sure if that email is a real one. How can I make sure the email is real?

Here is what I did:

void initState() {
  late final _email = TextEditingController();
  late final _firstName = TextEditingController();
  late final _familyName = TextEditingController();
  late final _password = TextEditingController();
  super.initState();
}

@override
  void dispose() {
    _email.dispose();
    _firstName.dispose();
    _familyName.dispose();
    _password.dispose();
    super.dispose();
  }

after initstate and dispose , I created a class extends StatefulWidget, which had inside it 4 TextFields with 4 controllers and a button which the onpressed function is :

onPressed: () async {
    final email = _email.text.trim();
    final password = _password.text.trim();
    try {
      FirebaseFirestore.instance;
      final usercredential =
          await FirebaseAuth.instance
              .createUserWithEmailAndPassword(
                  email: email,
                  password: password);
      final userEmail =
          usercredential.user?.email;
      print(usercredential);
      if (userEmail == email) {
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(
            builder: (context) =>
                UsernameAndPhonenumber(),
          ),
          (route) => route.isFirst,
        );
      } else {
        setState(() {
          msg = "Please verify your email";
        });
      }
    } on FirebaseAuthException catch (e) {
      if (e.code == "channel-error") {
        setState(() {
          msg = "Can't be empty";
        });
      } else if (e.code ==
          "weak-password") {
        print("weak password");
        setState(() {
          msg =
              "The password provided is too weak.";
        });
      } else if (e.code ==
          "email-already-in-use") {
        print(
            "The account already exists for that email.");
        setState(() {
          msg =
              "The account already exists for that email.";
        });
      } else if (e.code ==
          "invalid-email") {
        print("invalid email");
        setState(() {
          msg = "invalid email";
        });
      } else {
        print("SOME ERROR HAPPENED HERE");
        print(e.code);
        print(e.code);
        setState(() {
          msg = "unknown erorr occured";
        });
      }
    }
  },
Share edited Feb 10 at 15:00 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Feb 10 at 14:03 Abdlrhman BashirAbdlrhman Bashir 154 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

To know whether an email address is real (and whether the user has access to the mailbox for that address) you have to send a message to it. In Firebase this is known as email verification and is tied to the emailVerified property of the user account. Email verification is built into the product in two ways:

  1. If you use email+password authentication, you can ask Firebase to send a message to the email address with a link in. When the user clicks that link, it opens a web page and the emailVerified property in their profile will be set to true.
  2. You can use email link sign-in, which sends them a similar link, but when they click it it both sets the emailVerified property in their profile to true and signs them in to Firebase.

You'll typically want to check the value of the emailVerified property after the user has signed in, and only allow them access to (sensitive data in) the app when it is set to true.

Also see:

  • Verify a user's email address before confirming registration, with Flutter and Firebase
  • Firebase email/password authentication - how to require email verification?
  • Security rule to only allow write for users with verified emails (for securing access to the Realtime Database based on the email being verified)
  • (Firebase) Firestore security rules - allow if email verified without custom tokens? (for securing access to Firestore based on the email being verified)

There is no way to quickly know if the user has supplied a real email address. The only way to know for sure is to send an email to the address and ask them to respond to it. That's why Firebase offers email links as a form of verification. Read the documentation about that. It says provides:

The ability to authenticate a user while also verifying that the user is the legitimate owner of an email address.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>