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

javascript - How to create new user document in firestore - Stack Overflow

programmeradmin1浏览0评论

I am trying to implement this logic, when user successfully registers, app creates a document with id=user.email in firestore. For that, I created following security rule in firestore:

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

and following code in my app:

const { email, password, name, lastName } = value;

firebase
  .auth()
  .createUserWithEmailAndPassword(email, password)
  .then(() => {
        firestore.collection('users').doc(email).set({
          name, lastName
        })
        .then(function(docRef) {
          console.log("Document written with ID: ", docRef.id);
        })
        .catch(function(error) {
          console.error("Error adding document: ", error);
        });
        this.props.navigation.navigate('Main')
      })
  .catch(error => alert(error))

when I run my app, I am getting following error:

Error adding document: , [FirebaseError: Missing or insufficient permissions.]

I am trying to implement this logic, when user successfully registers, app creates a document with id=user.email in firestore. For that, I created following security rule in firestore:

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

and following code in my app:

const { email, password, name, lastName } = value;

firebase
  .auth()
  .createUserWithEmailAndPassword(email, password)
  .then(() => {
        firestore.collection('users').doc(email).set({
          name, lastName
        })
        .then(function(docRef) {
          console.log("Document written with ID: ", docRef.id);
        })
        .catch(function(error) {
          console.error("Error adding document: ", error);
        });
        this.props.navigation.navigate('Main')
      })
  .catch(error => alert(error))

when I run my app, I am getting following error:

Error adding document: , [FirebaseError: Missing or insufficient permissions.]
Share Improve this question edited Aug 2, 2019 at 5:29 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Aug 2, 2019 at 5:08 N NN N 1,6384 gold badges28 silver badges46 bronze badges 1
  • Did you solve the problem? I have the similar rule allow write: if request.auth.token.email == userId; and as a result I have the same error. – andreich Commented Nov 21, 2019 at 7:38
Add a ment  | 

1 Answer 1

Reset to default 8

It's not a good idea to use the email address of the user as a unique identifier, as that can change over time. It's better to use the unique user id (uid) assigned by Firebase Authentication.

Rules:

match /users/{userId} {
    allow read: if request.auth != null;
    allow write: if request.auth.uid == userId;
}

Code:

firebase
  .auth()
  .createUserWithEmailAndPassword(email, password)
  .then(userCredential => {
        firestore.collection('users').doc(userCredential.user.uid).set({
          name, lastName
        })

This is far more mon, makes it easier to write security rules, and is resistant to email changes.

发布评论

评论列表(0)

  1. 暂无评论