Firebase身份验证:在注册之前验证电子邮件

时间:2018-04-07 14:55:15

标签: android firebase firebase-realtime-database firebase-authentication

以下是我的想法:注册后,会向用户发送一封电子邮件。用户验证电子邮件后,具有用户信息的新帐户将添加到Firebase。如果没有,则不会添加该帐户。但我不知道该怎么做。

这是我的代码:

auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    FirebaseUser user = auth.getCurrentUser();
                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
                    DatabaseReference usersRef = ref.child("shop").child("Users");
                    String userId = usersRef.push().getKey();
                    final Map<String, Object> dataMap = new HashMap<String, Object>();
                    Users temp=new Users(email,user.getUid(),fullname);
                    usersRef.child(user.getUid()).setValue(temp);
                } else {
                    email_txt.setError(
                        "The email address is already in use by another account.");
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

最简单的方法是允许用户“被创建”,但在登录后直接检查用户的电子邮件是否已经过验证;如果没有,请将它们发送到应用中的验证页面,警告他们在没有经过验证的电子邮件地址的情况下无法访问该应用。

然后,您可以删除未使用cron作业定期调用的firebase函数验证的帐户。

这是我使用的每月运行的管理功能(它使用的是打字稿而不是javascript):

export const AutoDeleteUser = functions.https.onRequest((req, res) => {
    const currentTime = new Date().getTime();
    const month1 = currentTime - 2628000000;
    const month2 = currentTime - (2628000000 * 2);
    /* month1 and month2 are set so only users whose accounts are between one and two months old should be deleted
     * signupDate is how I find out how old the account is or in this case, how long its existed without verificaion
    */

    return userRef.orderByChild("signupDate").startAt(month2).endAt(month1).once("value")
    .then((snap) => {
        const uidList : string[] = [];
        snap.forEach(childSnap => {
            const uid = childSnap.key;
            uidList.push(uid);
            return admin.auth().getUser(uid).then((userRecord) => {
                if(userRecord.emailVerified) {
                    return false;
                } else {
                    return admin.auth().deleteUser(uid).then(() => {
                        return true;
                    },(reason) => {
                        console.error("Error deleting user " + uid + " : " + reason);
                        return false;
                    });
                }
            }, (reason) => {
                console.error("Error getting user " + uid + " : " + reason);
            }).then((wasDeleted) => {
                if(wasDeleted) {
                    console.log("User auto-deleted: " + uid);
                }
            });
        });
        return uidList
    }).then((uidList) => {
        res.send(uidList.join());
    });
});

为了节省一些能量并跟踪帐户存在的时间,我还添加了一个signupDate节点来索引用户,并阻止该功能包括其帐户已经过电子邮件验证测试的用户。< / p>

从测试中可以毫无障碍地进行测试,但这实际上只是解决问题的一种解决方案,同时保留了firebase的所有管理工具,如电子邮件验证。

我还有一个功能,可以在此处删除帐户时从数据库中删除用户的帐户,以供参考(如果有帮助):

export const DeleteUser = functions.auth.user().onDelete((event) => {
    const uid = event.data.uid;
    return userRef.child(uid).remove((error) => {
        if (error !== null) {
            console.error("Error deleting user",error);
        }
    }).then(() => {
        console.log("User Data Deleted");
    });
});