Flutter and Firestore does not have user informati

2019-04-25 12:04发布

Using flutter, I have installed the firebase-auth and firestore packages and am able to both authenticate with firebase auth and make a call into firestore as long as I don't have any rules around the user.

I have a button that calls _handleEmailSignIn and I do get a valid user back (since they are in the Firebase Auth DB)

import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

final FirebaseAuth _auth = FirebaseAuth.instance;

void _handleEmailSignIn(String email, String password) async {
  try {
    FirebaseUser user = await _auth.signInWithEmailAndPassword(
        email: email, password: password);

    print("Email Signed in " + user.uid);  // THIS works
  } catch (err) {
    print("ERROR CAUGHT: " + err.toString());
  }
}

I then have another button that calls this function to attempt to add a record into the testing123 collection.

Future<Null> _helloWorld() async {
  try {
    await Firestore.instance
        .collection('testing123')
        .document()
        .setData(<String, String>{'message': 'Hello world!'});
    print('_initRecord2 DONE');
  } catch (err) {
    print("ERROR CAUGHT: " + err.toString());
  }
}

Now this works as long as I don't have any rules around checking the request user. This works...

service cloud.firestore {
  match /databases/{database}/documents {
    match /testing123auth/{doc} {
        allow read, create
    }
  }
}

This does not which gives PERMISSION_DENIED: Missing or insufficient permissions. when I want to make sure I have the authenticated user I did with _handleEmailSignIn.

service cloud.firestore {
  match /databases/{database}/documents {
    match /testing123auth/{doc} {
        allow read, create: if request.auth != null;
    }
  }
}

I suspect that the firestore request is not including the firebase user. Am I meant to configure firestore to include the user or is this supposed to be automatic as part of firebase?

1条回答
地球回转人心会变
2楼-- · 2019-04-25 12:24

I meant to post a comment, but my rep is not enough.

Did you find a solution to the error? I would have suggested to make the rule like:

service cloud.firestore {
  match /databases/{database}/documents {
   match /testing123auth/{documents=**} {
    allow read, create: if true;
    }
  }
}

Or, better yet, limit the scope of the user:

service cloud.firestore {
  match /databases/{database}/documents {
    match /testing123auth/{userId} {
      allow read, create: 
        if (request.auth.uid != null &&
            request.auth.uid == userId); // DOCUMENT ID == USERID
      } // END RULES FOR USERID DOC

      // IF YOU PLAN TO PUT SUBCOLLECTIONS INSIDE DOCUMENT:
      match /{documents=**} {
        // ALL DOCUMENTS/COLLECTIONS INSIDE THE DOCUMENT
        allow read, write:
          if (request.auth.uid != null &&
            request.auth.uid == userId);
      } // END DOCUMENTS=**
    } // END USERID DOCUMENT
  }
}
查看更多
登录 后发表回答