1

I am stuck at the adding an authenticated user to a firestore 'users' collection.

Unhandled Exception: [cloud_firestore/not-found] Some requested document 
was not found.

User signs in via Google:

class GoogleSignInProvider extends ChangeNotifier {
final GoogleSignIn _googleSignIn = GoogleSignIn();

GoogleSignInAccount _user;

GoogleSignInAccount get user => _user;

AuthService auth = AuthService();

Future googleSignIn(BuildContext context) async {
try {
  final googleUser = await _googleSignIn.signIn();
  if (googleUser == null) return;

  _user = googleUser;

  final googleAuth = await googleUser.authentication;

  final AuthCredential credential = GoogleAuthProvider.credential(
      idToken: googleAuth.idToken, accessToken: googleAuth.accessToken);

  await FirebaseAuth.instance.signInWithCredential(credential);
  } catch (e) {
  print(e.toString());
}

final User currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null)
usersRef.add(currentUser.uid);

Navigator.of(context).pushReplacement(
    CupertinoPageRoute(builder: (_) => TabScreen()));
notifyListeners();
}

However, no matter what and how I tried the authentication firebase id is not added to the usersRef (the firestore collection). How do I fix it?

My firestore rules are:

match /users/{userId}/{documents=**} {
  allow read: if request.auth.uid != null;
  allow write, update, create, delete: if isOwner(userId);
}
 function isOwner(userId) {
return request.auth.uid == userId;
}

Help appreciated very much!

4
  • 1
    could you show your actual response from firestore and the dart firestore code? Commented Sep 4, 2021 at 17:59
  • Hi, @Mariano, the app signs in, however when i need to edit profile, the firestore gives above error: no docs to update. This is bcoz authenticated user is not in the firestore collection. Commented Sep 4, 2021 at 19:43
  • so, what's your question? Are you trying to create a firebase collection? you can't modify something is not created yet. Firebase auth can only modify Firebase auth records and that's it. If you don't show what usersRef has, we can't know what's going on. Commented Sep 4, 2021 at 20:26
  • aan, i see. i do have firestore collection. i need to add the authenticated user to it. Commented Sep 5, 2021 at 13:25

2 Answers 2

3

so this solved my issue:

final User currentUser = FirebaseAuth.instance.currentUser;
  if (currentUser != null)
    await usersRef.doc(currentUser.uid).set({'email': 
user.email, 'username': user.displayName, 'photoUrl': 
user.photoURL});

I think, I was overthinking it..

Thanks, ppl for the directions and help!

Sign up to request clarification or add additional context in comments.

Comments

0

Do this.

Map data = {};
FirebaseFirestore.instance
    .collection('usersCollection')
    .doc(currentUser.uid)
    .set(data);

in place of usersRef.add(currentUser.uid);

Use set when you have doc id and use add when you want an auto generated id.

3 Comments

getting type error. Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'... my google sign in is not setting uid, not fetching email and username of the google user. don't understand why? could it be cache data? but i did clean the firestore users data... uninstalled app several times
Replace data with what you want to add as data. eg. {name: 'Client name', address: 'xxx'}
@HaKim I think this should resolve your issue. Any luck trying it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.