How to fix 'user unauthenticated' firebase storage when authenticating with phone

722

I finally learned that enforcing the AppCheck storage access was not recommended this early in development. Unenforced the AppCheck for storage and it worked! Will be watching their videos from now on!

Share:
722
HaKim
Author by

HaKim

Updated on December 31, 2022

Comments

  • HaKim
    HaKim over 1 year

    Very new to Firestore and can't seem to fix the problem I have. I am registering the user with the phone, then prompt the user to edit data in app. However, I am encountering the error, and don't know how to fix it.

    The error:

    I/flutter ( 7502): [firebase_storage/unauthenticated] User is unauthenticated. Authenticate and try again.

    W/Firestore( 7502): (23.0.3) [WriteStream]: (b2291d0) Stream closed with status: Status{code=NOT_FOUND, description=No document to update:

    projects/blahblah/databases/(default)/documents/users/CrxXOi8vajhUYfevbPbMRjAHQqrv5, cause=null}.

    E/flutter ( 7502): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)]

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

    Authentication works fine, as I see the user with the uid in users. However, not in the Firestore collection 'Users'. My rules are very simple:

    rules_version = '2';
    service cloud.firestore {
    match /databases/{database}/documents {
    
    match /users/{id} {
    allow read, delete, update, create: if request.auth != null;
    }}}
    

    My storage rules are set to:

     rules_version = '2';
      service firebase.storage {
       match /b/{bucket}/o {
         match /{allPaths=**} {
          allow read, write: if request.auth != null;
         }}
    

    edit profile method is:

      String currentUid() {
     return firebaseAuth.currentUser.uid;
     }
    
     updateProfile(
      {File image,
      String username,
      String email,
      String sex,
      String dob,
      String phone}) async {
    DocumentSnapshot doc = await usersRef.doc(currentUid()).get();
    var users = UserModel.fromJson(doc.data());
    users.username = username;
    users.email = email;
    users.phone = phone;
    users.dob = dob;
    users.sex = sex;
    
    if (image != null) {
      users.photoUrl = await uploadImage(profilePic, image);
    }
    await usersRef.doc(currentUid()).update(
        {'username': username, 'photoUrl': users.photoUrl, 'phone': phone,
          'dob': dob, 'email': email, 'sex': sex});
    return true;
    }
    

    Help appreciated very much!

  • HaKim
    HaKim over 2 years
    Hello! @Huthaifa They are set exactly as you have posted here.
  • Admin
    Admin over 2 years
    Please add further details to expand on your answer, such as working code or documentation citations.
  • Andreas Lymbouras
    Andreas Lymbouras about 2 years
    If you go to Firebase console -> Project settings -> App check, you can enforce App Check on specific services like Storage. This enforcement was causing my authentication error as well.