首页 > 解决方案 > Firestore 安全规则 email_verified 不起作用

问题描述

如果我创建一个新用户createUserWithEmailAndPassword,即使我还没有验证邮件,该用户已经登录。他的.emailVerified === false,直到这里一切都好。

现在,我去邮件,使用链接验证它,回到网络应用程序,它仍然是.emailVerified === false所以我刷新页面,现在.emailVerified === true
所以我尝试联系这个文档:

  public async getPublicUserDetails() {

    const currentUserId = this._angularFireAuth.auth.currentUser.uid;

    try {

      const docRef = this._angularFirestore.collection("users").doc(currentUserId).ref;
      const doc = await docRef.get();

      if (!doc.exists) {
        return null;
      }

      return doc.data() as IPublicUserDetailsDto;
    }
    catch (error) {

      console.error("User " + currentUserId + " details get failed! " + JSON.stringify(error));
      throw error;
    }
  }

它捕获一个异常,说我没有访问文档所需的权限。

我使用的 Firestore 规则是:

rules_version = '2';
service cloud.firestore {

    function dbDocs() { return /databases/$(database)/documents; }
    function isSignedIn() { return request.auth != null && request.auth.uid != null; }
    function isEmailVerified() { return isSignedIn() && request.auth.token.email_verified; }
    function isCurrUser(uid) { return isSignedIn() && request.auth.uid == uid; }
    function userExists(uid) { return exists(/databases/$(database)/documents/users/$(uid)); }

    match /databases/{database}/documents {

        match /users {    

            match /{userId} {
                allow read: if isEmailVerified();
                allow write: if isEmailVerified() && isCurrUser(userId);
            } 
        }
    }
}

我可以无限次刷新页面,但只有当我再次注销和登录allow read将行 替换为

match /{userId} {
    allow read: if isSignedIn(); // replace this
    allow write: if isEmailVerified() && isCurrUser(userId);
}

结论:似乎request.auth.token.email_verified没有反映FirebaseAuth服务内部提供的值,因为它似乎只有在我注销并重新登录时才会刷新。

有人能帮助我吗?谢谢大家!

标签: firebasegoogle-cloud-firestorefirebase-authenticationangularfirefirebase-security

解决方案


推荐阅读