firebase 安全规则,更改密码后如何拒绝权限

firebase security rules , how deny permission after change password

我正在构建一个网络应用程序并使用 firebase 来存储数据,我试图让用户在密码更改时注销到所有设备或拒绝用户“读取”和“在已在不同设备上进行身份验证但用户已更改密码的 firestore 规则上写入“。

我想这样做是因为万一用户帐户被泄露并且原始用户更改了密码,如果保持日志记录,入侵者仍然可以访问数据库的读写权限

Revoke refresh tokens 密码重置还会撤销用户现有的令牌;但是,在这种情况下,Firebase 身份验证后端会自动处理撤销。撤销时,用户将注销并提示重新进行身份验证。

我不明白如何使用令牌,也许有一种方法可以检查“if firebase token === user client token”以查看另一台设备上的用户是否使用旧令牌并拒绝写入和密码更改后阅读。

我的代码是这样的:

//重置密码:

const resetPass = () =>{
    let newPassword = "testing3";

    updatePassword(user, newPassword).then(() => {
        console.log('password updated = '+ newPassword)
        logout () //logout logic

    }).catch((error) => {
        console.log('password not updated '+ error)
    })
}

firestore 规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

抱歉,如果我遗漏了什么,我还在学习,在此先感谢。

在删除用户账号或更改用户密码等场景中,我们考虑撤销用户的刷新令牌。在这种情况下,Firebase 会自动处理令牌撤销。一旦以这种方式撤销了刷新令牌,就不能使用它来获取新的 ID 令牌。因此,及时会提示用户重新登录,并获得一对新的 ID 和刷新令牌。

因此,您可以修改安全规则,将 ID 令牌颁发时间与数据库中存储的撤销时间进行比较。您可以通过以下方式匹配请求数据上的 auth.uid 变量和用户 ID 来设计您的安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

您也可以参考documentation