最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cloud Firestore with Authentication - Stack Overflow

programmeradmin1浏览0评论

I have been looking for a way to use Authentication with Firebase's new Cloud Firestore. I know it's possible, but there is no good guide both in the Documentation and other places.

Could someone please explain how to provide an Authentication UID when get()ing data from Cloud Firestore?

Here are my security rules:

service cloud.firestore {
  match /users/{userID} {
    allow read, write: if request.auth.uid == userID;
  }
}

And here is my current code:

var db = firebase.firestore();
var docRef = db.collection("users").doc(uid); //lets say "uid" var is already defined
docRef.get().then(function(doc) {
  if (doc.exists) {
      console.log("Document data:", doc.data());
  } else {
      console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

The structure of my database is just a "users" collection at root, then a document for each user (named after the UID). I want to get the document with the user's UID.

Of course, this gives the error "Missing or insufficient permissions," which is expected, because of the security rules.

This question may seem simple, but if anyone can find some good documentation on this, that would be great!

I have been looking for a way to use Authentication with Firebase's new Cloud Firestore. I know it's possible, but there is no good guide both in the Documentation and other places.

Could someone please explain how to provide an Authentication UID when get()ing data from Cloud Firestore?

Here are my security rules:

service cloud.firestore {
  match /users/{userID} {
    allow read, write: if request.auth.uid == userID;
  }
}

And here is my current code:

var db = firebase.firestore();
var docRef = db.collection("users").doc(uid); //lets say "uid" var is already defined
docRef.get().then(function(doc) {
  if (doc.exists) {
      console.log("Document data:", doc.data());
  } else {
      console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

The structure of my database is just a "users" collection at root, then a document for each user (named after the UID). I want to get the document with the user's UID.

Of course, this gives the error "Missing or insufficient permissions," which is expected, because of the security rules.

This question may seem simple, but if anyone can find some good documentation on this, that would be great!

Share Improve this question edited Nov 18, 2017 at 18:41 yummypasta asked Nov 18, 2017 at 18:24 yummypastayummypasta 1,4782 gold badges19 silver badges38 bronze badges 6
  • Do you want to get all users or only yours. – Hareesh Commented Nov 18, 2017 at 18:30
  • 1 Please provide an MCVE (read the link, it is quite useful). E.g. What does docRef point to? Are you signing the user in? – Frank van Puffelen Commented Nov 18, 2017 at 18:30
  • @FrankvanPuffelen Okay, I edited the question to fill in missing information. Yes, I have done authentication with Google already. I have the UID, I just need to know what to do with it – yummypasta Commented Nov 18, 2017 at 18:33
  • @Hareesh I added information on the structure of my database. I have a "users" collection at root, which has documents (named after the UID's). I want to have the user with the corresponding UID to access their own doc. – yummypasta Commented Nov 18, 2017 at 18:35
  • ok how you linked your cities collection to users? – Hareesh Commented Nov 18, 2017 at 18:39
 |  Show 1 more comment

2 Answers 2

Reset to default 10

I was just confused about how UIDs work. I was under the impression that you have to supply a UID and an auth token to database operation.

You don't actually pass in a UID to database operations. The firebase object stores authentication state. You can simply sign in (check the Firebase docs to read how to do this), and after the action is completed (use a .then promise statement) you can simply do your operation.

Additionally, to have users not sign in every time they visit your app, you can have the `firebase object's authentication state persistent.

Your rule should be inside match /databases/{database}/documents

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userID} {
        allow read, write: if request.auth.uid == userID;
   }
  }
}
发布评论

评论列表(0)

  1. 暂无评论