简体   繁体   English

Firebase 电子邮件/密码身份验证 + 验证怪异

[英]Firebase email/password auth + verification weirdness

The use of email verification + email/password authentication doesn't quite work for services that absolutely need email verification before the user can begin using the service.对于在用户开始使用服务之前绝对需要电子邮件验证的服务,使用电子邮件验证 + 电子邮件/密码身份验证不太适用。

Let me explain with an example for Google sign in first .让我先用一个谷歌登录的例子来解释。

First, the user signs into their Google account (say email is op@op.com ), and authorizes your app.首先,用户op@op.com他们的 Google 帐户(假设电子邮件是op@op.com ),并授权您的应用程序。 Then you create a credential using the tokens received through that, and exchange those tokens with Firebase to log the user into Firebase.然后您使用通过它收到的令牌创建一个凭证,并与 Firebase 交换这些令牌以将用户登录到 Firebase。 The user needs to exist in Firebase for you to use Firebase' email verification service (because the only way to get info on whether an email is verified is to check the currentUser object in the client, so you need a logged in user to check if their email is verified. You can't call an Auth method with an email address to check if it's verified or not).用户需要存在于 Firebase 中才能使用 Firebase 的电子邮件验证服务(因为获取有关电子邮件是否已验证的信息的唯一方法是检查客户端中的currentUser对象,因此您需要一个登录用户来检查是否他们的电子邮件已通过验证。您无法使用电子邮件地址调用Auth方法来检查它是否已验证)。 So once you log the user into Firebase, you send them a verification link, and all is good.因此,一旦您将用户登录到 Firebase,您就向他们发送了一个验证链接,一切都很好。 You can configure the view on your client by checking the user object for email verification.您可以通过检查电子邮件验证的用户对象来配置客户端上的视图。 An important point to note here is that some other user who knows this user's email address cannot register using op@op.com on your service: this is because they need to sign into Google with that email to register.这里需要注意的重要一点是,一些知道此用户电子邮件地址的其他用户无法在您的服务上使用op@op.com进行注册:这是因为他们需要使用该电子邮件登录 Google 才能注册。

Facebook is similar to Google sign-in in this regard.在这方面,Facebook 类似于 Google 登录。

However, for email/password , anyone can take someone else's email and create an account with it!但是,对于 email/password ,任何人都可以使用别人的电子邮件并用它创建一个帐户! And since you cannot send a verification link before the user is registered in Firebase, you're essentially letting anyone in the world "block" email addresses on your service.由于您无法在用户注册 Firebase 之前发送验证链接,因此您实际上是在让世界上的任何人“阻止”您服务中的电子邮件地址。 I was initially trying to ensure email verification before the email is registered into Firebase, but quickly realized that I need the user in Firebase to do any email verification.我最初试图在电子邮件注册到 Firebase 之前确保电子邮件验证,但很快意识到我需要 Firebase 中的用户进行任何电子邮件验证。

Am I missing something, or is this the expected behavior?我错过了什么,还是这是预期的行为? If this is really how it works, then I might just not allow email/password login in my app.如果这真的是它的工作原理,那么我可能只是不允许在我的应用程序中使用电子邮件/密码登录。

Side note : another idea I had was to do verification by sending them a 6-digit code, and maintain my own verification system in Firebase.旁注:我的另一个想法是通过向他们发送 6 位代码来进行验证,并在 Firebase 中维护我自己的验证系统。 But then I can't add any security rules to it, since any client without a logged in user would need access to it ==> potential system abuse.但是我无法向它添加任何安全规则,因为任何没有登录用户的客户端都需要访问它 ==> 潜在的系统滥用。

Thanks in advance for attempting to read through the long explanation.预先感谢您尝试通读冗长的解释。

So even though an account can be created unverified, you can still block user access using security rules.因此,即使可以在未经验证的情况下创建帐户,您仍然可以使用安全规则阻止用户访问。 The latter is what matters and controls access.后者是重要的并控制访问。 Here is an example how you can do so with realtime database rules:以下是如何使用实时数据库规则执行此操作的示例:

{
  "rules": {
    "users": {
      "$user": {
        ".read": "auth.token.email_verified == true && auth.uid === $user",
        ".write": "auth.token.email_verified == true && auth.uid === $user"
      }
    }
  }
}

You can also do this on your own if you are verifying the ID token on your server by parsing the token payload and inspecting the email_verified .如果您通过解析令牌有效负载并检查email_verified来验证服务器上的 ID 令牌,您也可以自行执行此email_verified

So even if the user account is created, unless the user is verified, they will not have access to your app/site data.因此,即使创建了用户帐户,除非用户经过验证,否则他们将无法访问您的应用程序/站点数据。

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

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