在确认注册之前验证用户的电子邮件地址,使用 Flutter 和 Firebase [英] Verify a user's email address before confirming registration, with Flutter and Firebase

查看:19
本文介绍了在确认注册之前验证用户的电子邮件地址,使用 Flutter 和 Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Firebase 作为后端身份验证服务,创建了一个适用于我的 Flutter 应用的登录和注册屏幕.我可以很好地在登录、注册和重置密码屏幕之间切换.

I've created a login and registration screen that works with my Flutter app, using Firebase as the backend authentication service. I'm able to switch between the login, registration and reset password screens well.

问题

目前,注册屏幕接受输入的任何电子邮件地址,无论它是否真实.例如,如果我输入 gvevg@gverwbgw.com,它将允许用户注册.这显然是一个问题,涉及到虚假帐户和垃圾邮件等.

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. This is obviously an issue, when it comes to fake accounts and spam etc.

宗旨

我基本上希望能够编辑我的代码,自动生成电子邮件地址验证电子邮件,防止用户在他们的电子邮件地址被验证之前登录.我编写的代码使用 FutureFirebaseAuthasync/await 来实现这一点.

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.

我当前的代码

首先,我定义了一个 AuthBase 抽象类,它创建了createUserWithEmailAndPassword"函数(等等),如下所示:

Firstly, I define an AuthBase abstract class, that creates the 'createUserWithEmailAndPassword' function (amongst others) as follows:

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

然后,我创建了一个 Auth 函数,它实现了 AuthBase,从 Firebase 获取当前用户并创建注册Future 函数,如下所示:

Then, I create an Auth function, that implements AuthBase, gets the current user from Firebase and creates the registration Future function, as follows:

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

我的问题

如何编辑我的代码,以便它允许我为任何想要使用电子邮件登录的用户自动实施电子邮件验证?我相信 sendEmailVerification() 函数必须使用 FirebaseUser,虽然我不知道如何在这里实现它.我将不胜感激任何帮助.谢谢!

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. I would appreciate any help. Thanks!

推荐答案

电子邮件+密码认证只需要用户知道电子邮件+密码的组合即可.它本身不需要验证电子邮件地址才能登录.如果您希望在允许访问其他数据之前验证电子邮件地址,您可以通过 检查用户的令牌以获取email_verified 声明,例如在您的数据库的安全规则中.

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.

另见:

这篇关于在确认注册之前验证用户的电子邮件地址,使用 Flutter 和 Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆