简体   繁体   English

在确认注册之前验证用户的电子邮件地址,使用 Flutter 和 Firebase

[英]Verify a user's email address before confirming registration, with Flutter and Firebase

I've created a login and registration screen that works with my Flutter app, using Firebase as the backend authentication service.我创建了一个适用于我的 Flutter 应用程序的登录和注册屏幕,使用 Firebase 作为后端身份验证服务。 I'm able to switch between the login, registration and reset password screens well.我能够很好地在登录、注册和重置密码屏幕之间切换。

The Issue问题

At the moment, the registration screen accepts any email address that is entered, whether or not it is real.目前,注册屏幕接受输入的任何电子邮件地址,无论它是否真实。 For example, if I were to type in gvevg@gverwbgw.com, it would allow the user to register.例如,如果我输入 gvevg@gverwbgw.com,它将允许用户注册。 This is obviously an issue, when it comes to fake accounts and spam etc.当涉及到虚假帐户和垃圾邮件等时,这显然是一个问题。

The Aim目的

I would basically like to be able to edit my code, to automatically generate an email address verification email, which prevents the user from signing in, before their email address has been verified.我基本上希望能够编辑我的代码,自动生成电子邮件地址验证电子邮件,防止用户在他们的电子邮件地址被验证之前登录。 The code I have made uses a Future , FirebaseAuth and async/await to make this happen.我编写的代码使用FutureFirebaseAuthasync/await来实现这一点。

My Current Code我当前的代码

Firstly, I define an AuthBase abstract class, that creates the 'createUserWithEmailAndPassword' function (amongst others) as follows:首先,我定义了一个 AuthBase 抽象类,它创建了“createUserWithEmailAndPassword”函数(等等),如下所示:

abstract class AuthBase {
  Stream<User> get onAuthStateChanged;
  Future<User> currentUser();
  Future<User> createUserWithEmailAndPassword(String email, String password);
}

Then, I create an Auth function, that implements AuthBase, gets the current user from Firebase and creates the registration Future function, as follows:然后,我创建了一个 Auth 函数,它实现了 AuthBase,从 Firebase 获取当前用户并创建注册Future函数,如下所示:

class Auth implements AuthBase {
  final _firebaseAuth = FirebaseAuth.instance;

  // This creates the user ID token variable (if one doesn't already exist) which is then populated using one of the login methods.
  User _userFromFirebase(FirebaseUser user) {
    if (user == null) {
      return null;
    }
    return User(uid: user.uid);
  }

  // This helps to get the user from Google Firebase, noting if there is or isn't a user with those login details already.
  @override
  Stream<User> get onAuthStateChanged {
    return _firebaseAuth.onAuthStateChanged.map(_userFromFirebase);
  }

  // This identifies the current user, who is logged in at the time.
  @override
  Future<User> currentUser() async {
    final user = await _firebaseAuth.currentUser();
    return _userFromFirebase(user);
  }

  // This creates the user account for an email-and-password sign-in, with firebase, if it doesn't already exist.
  @override
  Future<User> createUserWithEmailAndPassword(
      String email, String password) async {
    final authResult = await _firebaseAuth.createUserWithEmailAndPassword(
        email: email, password: password);
    return _userFromFirebase(authResult.user);
  }
}

My Question我的问题

How do I edit my code, so that it allows me to implement email verification automatically for any user that wants to sign in with email?如何编辑我的代码,以便它允许我为任何想要使用电子邮件登录的用户自动实施电子邮件验证? I believe the sendEmailVerification() function must use FirebaseUser , although I am not sure how to implement it here.我相信sendEmailVerification()函数必须使用FirebaseUser ,尽管我不确定如何在这里实现它。 I would appreciate any help.我将不胜感激任何帮助。 Thanks!谢谢!

Email+password authentication requires nothing more than that the user knows the combination of email+password.电子邮件+密码认证只需要用户知道电子邮件+密码的组合即可。 It doesn't in itself require the email address to be verified to sign in. If you want the email address to be verified before allowing access to other data, you can do that by checking the user's token for the email_verified claim for example in the security rules of your database.它本身并不需要验证电子邮件地址才能登录。如果您希望在允许访问其他数据之前验证电子邮件地址,您可以通过检查用户的令牌来实现email_verified声明,例如在数据库的安全规则。

Also see:另见:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM