向现有用户发送验证电子邮件 [英] Sending Verification email to existing users

查看:59
本文介绍了向现有用户发送验证电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有现有用户群的Web应用程序.最初并未在登录流程中实施电子邮件验证.
我已经成功添加了用于发送所有新注册验证电子邮件的代码,但我还想创建一个小页面(或模式),在该页面上会向当前用户显示一个按钮,该按钮会将验证链接发送到他们的收件箱

I am working on a web app with an existing user base. Email verification was not initially implemented in the sign in flow.
I have successfully added code for sending verification email for all new sign ups but I also wanted to make a small page (or modal) where current users would be shown a button that would send the verification link to their inbox

我使用创建用户的当前注册流程 createUserWithEmailAndPassword 我能够访问 user.user.sendEmailVerification 方法,但是找不到任何方法来实现此功能对于现有用户.

The current sign up flow where I created the user with createUserWithEmailAndPassword I was able to get access to the user.user.sendEmailVerification method to do so, but cannot find any way to access this method to implement the feature for existing users.

是否可以访问 sendEmailVerification 方法?

Is there a way to access the sendEmailVerification method after the user has been created?

我假设它将在 onAuthStateChange 触发器,但实施该操作会导致不良的UX(,因为我不想在用户每次登录时都提示他们)

我知道文档指出,我们可以使用 firebase.auth().currentUser 来获取当前用户,但是由于某些原因无法正常工作.另外,我在网上找到了引用,暗示不再使用该方法,他们提到要使用而是使用 onAuthStateChange 方法,这就是为什么我一直在研究这种方法的原因

I know the documentation states that we can use the firebase.auth().currentUser to get the current user but that, for some reason did not work. Also, I found references online suggesting to no longer use that method and they mentioned to use the onAuthStateChange method instead, which is why I was looking into that approach

推荐答案

不应在 onAuthStateChanged 事件中调用 sendEmailVerification(),因为它会爆炸电子邮件如果未验证用户的电子邮件,则会在每次加载页面时显示该信息.

The sendEmailVerification() should not be called in the onAuthStateChanged event because it would blast out an email on every page load if the user's email isn't verified.

如果 User.emailVerified 为false,则应该在页面上显示通知,其中包含向用户发送电子邮件的链接.

You should instead display a notification on the page if User.emailVerified is false that contains a link to send the user an email.

这是一个可行的示例:

// On page load watch for auth state changes
firebase.auth().onAuthStateChanged(function(user) {
    // If the user is logged in
    if (user) {
        // If the user's email isn't verified
        if (!user.emailVerified) {
            // Show the notification bar that informs the user that they need to validate
            // their email by clicking a link.  Let's pretend the link looks like this:
            // <a href="#" onclick="sendEmailVerification">Send me a verification email</a>
            showNotification();
        }
    }
});

// Function attached to your link's onclick event
function sendEmailVerification() {
    // Retrieve the current user
    const user = firebase.auth().currentUser;

    // If user's email is already verified, exit
    if (user.emailVerified) {
        return;
    }

    // Tell Firebase to send the verification email and discard the promise
    user.sendEmailVerification().then().catch();
}

Dharmaraj的回答很好,但这是一个完整的例子.

Dharmaraj's answer is good but this is a full example.

这篇关于向现有用户发送验证电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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