即使经过身份验证,Firebase 权限也被拒绝

问题描述 投票:0回答:1

我有一个包含这些规则的基本数据库

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }
    }
  }
}

我尝试从数据库路径 /users/+storageuid 读取

const storageuid = localStorage.getItem("uid");
export const testCall = ()=>{
    
    const dbRef = ref(getDatabase());
    const path = "/users/"+storageuid;
    
    get(child(dbRef, path)).then((snaphot) => {
        if (snapshot.exists()) {
            console.log(snapshot);
        } else {

        }
    }).catch((error) => {
        console.log(error);
    });
}

其中storageuid是我谷歌登录后的身份验证uid。我把它放在 localstorage.setItem("uid");

signInWithPopup(auth, provider)
    .then((result) => {
        // This gives you a Google Access Token. You can use it to access the Google API.
        const credential = GoogleAuthProvider.credentialFromResult(result);
        const token = credential.accessToken;
        // The signed-in user info.
        const user = result.user;
        // IdP data available using getAdditionalUserInfo(result)
        // ...
        console.log(user.uid);
        localStorage.setItem("uid",user.uid);
    }).catch((error) => {
        // Handle Errors here.
        const errorCode = error.code;
        const errorMessage = error.message;
        // The email of the user's account used.
        const email = error.customData.email;
        // The AuthCredential type that was used.
        const credential = GoogleAuthProvider.credentialFromError(error);
        // ...
    });

我的许可仍然被拒绝.. enter image description here 根据此页面,规则需要位于 $UID 中 https://firebase.google.com/docs/database/security

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    }
  }
}
javascript firebase-realtime-database firebase-security
1个回答
0
投票

您收到错误是因为您尝试通过

get(child(dbRef, path))
读取数据,但尚未设置允许 读取访问 的规则。您仅定义了 write-access

的规则
{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid" ,
        ".write": "$uid === auth.uid"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.