HaKim
HaKim

Reputation: 287

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

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!

Upvotes: 4

Views: 2934

Answers (4)

Shahed Oali Noor
Shahed Oali Noor

Reputation: 656

Follow these steps:

1. Firebase console -> Project settings -> App check, you can enforce App Check on specific services like Storage.

2. add to pubscec: firebase_app_check: ^0.2.1+14

3. rewrite void main as bellow:
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await FirebaseAppCheck.instance.activate();
  runApp(MyApp());
}

Upvotes: 0

Tanvir Ahmed
Tanvir Ahmed

Reputation: 681

I found a solution regarding this issue. pub get firebase_app_check: ^0.0.3 this package. and simply call this app check class-

await FirebaseAppCheck.instance.activate();

For my scenario I solved this issue this way.

Upvotes: 0

HaKim
HaKim

Reputation: 287

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!

Upvotes: 4

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12363

Your error is coming from Storage, not FireStore. Go to storage and edit the rules there.

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

Upvotes: 1

Related Questions