When refreshing the page, client Firebase seems to think I have an authenticated user, but the firestore firebase rules imply that I don't have an authenticated user.
I have a Firebase app where users sign in with email and password via signInWithEmailAndPassword
. I set persistence to browserLocalPersistence
. Normally when a user logs in, the onAuthStateChanged
method is called with the authenticated user object and the application runs normally. When the user refreshes the page, my onAuthStateChanged
method fires again with a non-null user, but all Firebase queries fail due to my security rules. What am I doing wrong there.
This is a reactjs application. Please let me know if I can provide any other answers that might inspire you.
My firebase rules:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
My login code:
1 2 3 4 5 6 7 8 |
|
My onAuthStateChanged code
1 2 3 4 5 6 7 |
|
P粉2587888312024-02-26 20:51:07
I think you forgot the comma between read
and write
, it should look like allow read, write: if isAuthenticated();
P粉8541192632024-02-26 14:51:34
I ended up having to implement an authentication provider so that it retained my user information.
New file AuthContext.tsx
1 2 3 4 |
|
New file AuthProvider.tsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Then I updated my index.tsx like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Then update the part of app.tsx where I call onAuthStateChanged to the following
1 2 3 4 5 6 7 |
|
After refreshing, it seems to retain all the necessary authentication information to extract the document.