如何在 Firebase 中获得电子邮件验证通知? [英] How to get notified in Firebase of email verification?

查看:31
本文介绍了如何在 Firebase 中获得电子邮件验证通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Firebase 身份验证进行电子邮件/密码注册.问题是我只想在使用的电子邮件地址也经过验证时接受用户进行身份验证.

I want to use Firebase Auth for email/password sign up. The problem is that I only want to accept the user as authenticated when the email address used is also verified.

通常我们可以使用侦听器来检查用户何时登录或不再登录:

Normally we can use a listener to check when a user is signed in or no longer signed in:

        Auth.auth().addStateDidChangeListener { (auth, user) in

问题是当用户单击验证电子邮件中的链接时不会触发此侦听器.只有在创建帐户后才会调用它.

The issue is that this listener is not triggered when the user clicks on the link in the verification email. It's only called once the account is created.

在等待用户点击链接时循环执行以下操作:

Doing the following in a loop works while waiting for the user to click the link:

        Auth.auth().currentUser!.reload() {
            if !Auth.auth().currentUser!.isEmailVerified {

但问题是我理想地希望管理侦听器回调中的所有身份验证内容,但是当电子邮件验证状态更改时不使用侦听器.

but the problem is I ideally want to manage all the auth stuff in the listener callback, but the listener is not used when the email validation state changes.

有什么我遗漏的吗?

我知道在移动设备上你可以使用深层链接,这样邮件中的链接会再次打开应用程序,但这不是我想要的,因为用户可以在他们的计算机上打开邮件,所以问题仍然需要解决.

I know on mobile you can use deep linking so the link in the mail would open the app again, but that's not what I am looking for as users could open the mail on their computer so the problem still needs to be solved.

目前我看到的唯一解决方案是自己发送一个 nsnotification 并在我处理所有身份验证代码的地方处理它,但感觉有点混乱.

For now the only solution I see is to send an nsnotification myself and handle it where I handle all the auth code, but it feels a bit messy.

推荐答案

这里有一些选项:

  1. 设置建立一个 RTDB 节点来跟踪用户的 emailVerified.
  2. 实施您自己的登录页面以进行电子邮件验证:https://firebase.google.com/docs/auth/custom-email-handler
  3. 在您的登录页面中,验证用户后,更新该 RTDB 节点中的 emailVerified.
  4. 在用户发送电子邮件验证的页面上,侦听该节点上的更改.更改时,调用 user.reload() 以更新用户的 emailVerifieduser.getIdToken(true) 以确保令牌已更新与最新的索赔.
  1. Set up a RTDB node to track the emailVerified of a user.
  2. Implement your own landing page for email verification: https://firebase.google.com/docs/auth/custom-email-handler
  3. In your landing page, when the user is verified, update emailVerified in that RTDB node.
  4. On the page where the user sent the email verification, listen to changes on that node. On change, call user.reload() to update emailVerified on the user and user.getIdToken(true) to make sure the token is udpated with the latest claims.

另一种方法是上述方法的变体,即添加 continueUrl 它将在默认电子邮件验证上显示一个继续按钮,以重定向到您拥有的页面,您知道该用户已通过验证.您还可以使用与上述类似的机制,通过该页面而不是登录页面上的实时数据库传递状态更改.

Another way is a variation of the above which is to add a continueUrl which will show a continue button on the default email verification to redirect to a page you own where you know the user is verified. You can also use a similar mechanism as above to pass the status change via real-time database on that page instead of the landing page.

这篇关于如何在 Firebase 中获得电子邮件验证通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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