-
Notifications
You must be signed in to change notification settings - Fork 927
The function createUserWithEmailAndPassword
doesn't grant the necessary permissions to access Firestore.
#5794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @CodoPixel I'll take a look at this. I don't see anything immediately obvious that you are doing wrong. I have a few questions though.
|
First of all, thank you for your answer.
// ConnectionPage.js
// same as the first, but the submit function is now the following
const submit = async (e) => {
e.preventDefault();
try {
const userCredentials = await createUserWithEmailAndPassword(
getAuth(),
email,
password
);
} catch (e) {
console.error(e.toString());
}
} And I'm using a context to transfer the state of the auth to my entire app // auth.js
import { createContext, useEffect, useContext } from "react";
import { doc, getFirestore, getDoc, setDoc } from "firebase/firestore";
import { auth } from "./firebase";
const AuthContext = createContext({});
export const AuthProvider = ({ children }) => {
useEffect(() => {
return auth.onAuthStateChanged(async (user) => {
if (!user) {
console.log("no user!");
return;
}
const token = await user.getIdToken();
// console.log("token", token);
// console.log("user", user);
// console.log("Trying to access firestore...");
const docRef = doc(getFirestore(), "users", user.uid);
const docSnapshot = await getDoc(docRef);
// I have never reached the following lines
if (docSnapshot.exists()) {
// the user had already an account
// so we can just modify a few fields
console.log("user already exists");
} else {
// the user didn't have an account,
// so we must add the document
console.log("user doesn't exist\nCreation of the document...");
const userInfo = {
uid: user.uid,
// etc.
};
// await docRef.set(userInfo);
await setDoc(docRef, userInfo)
.then(() => {
console.log(
`The document with id ${user.uid} has been created successfully.`
);
})
.catch((error) => {
console.error("error", error.toString());
});
}
});
}, []);
return <AuthContext.Provider value={{}}>{children}</AuthContext.Provider>;
};
export const useAuth = () => useContext(AuthContext); As requested, I have changed the rules to:
but I still have the same Runtime Error:
Now, about your other questions. I don't know how to use the Firestore emulator yet, so I'll need time to read the whole documentation, outside of my working hours. I will keep you posted. You also asked me to use // firebase.js
import { setLogLevel } from "firebase/app"
setLogLevel("debug")
const firebaseConfig = {}
const app = initializeApp(firebaseConfig)
// etc. which returns me (in the console) the following outputs:
Because I do not know what is the difference between // firebase.js
import { setLogLevel } from "firebase/app"
import { setLogLevel as firestoreLogLevel } from "firebase/firestore"
firestoreLogLevel("debug");
const firebaseConfig = {}
const app = initializeApp(firebaseConfig)
// etc. which tells me that in the console:
My answer is too big, sorry for that. |
That extra logging is perfect. It confirms that the query is being rejected due to security rules. This, however, should never happen if you've configured the "allow all" rules. I have a few things for you to try.
Good luck, and let me know how this goes. |
Hi, I had time to make tests and I have a situation I don't understand. As you recommended it, I checked if I was editing the rules of the right project. And indeed I was editing the correct rules. I have tested once again the app, still not working. However, I have created another project and now it's working! It's great but I don't understand why the first project is annoying me. Besides I want my web app to be stored in this project, not another one, this one only. This project is used by my android app and files are stored in Firebase storage, files that I'll need in my web app. As a consequence, I'm limited to this project and this is not possible for me to migrate the content of the Firebase storage to another project by hand. The only difference between the new project (working) and the old project (not working) is the use of the Firebase storage feature, the use of the firebase realtime database feature and the use of the App Check feature. Please, help me understand what I did wrong and how I could fix this. As I'm talking about App Check, note that I'm a complete noob in that subject, so if you also want to help there is this obscure subject that nobody wants to answer to: App Check with Flutter. Thank you |
Ok this is good information. Can you try temporarily disabling AppCheck on the |
Oh god, I'm sorry but I hate this App Check. Every time I try to use it, I have problems... My whole problem was coming from the App Check. I unenforced it from the Firestore feature, and it worked, then I enforced it again, and now it's not working lol. Do you have an explanation? How the permission can be insufficient whereas I have a debug token (which I registered correctly according to the documentation)... |
@CodoPixel You are using the JS SDK version 9.5.0, but Firestore started supporting AppCheck with SDK version 9.6.0. As a result, the Firestore client inside your SDK does not know how to communicate to AppCheck, and therefore if you turn on AppCheck enforcement you will not be able to access Firestore. Upgrading to the newer version should fix the problem automatically. I'll close this issue. If you continue to face any issues using the newer SDK please feel free to let us know. |
My environment
The problem
My goal: I want to add a new document in a collection entitled "users" in Firestore when a user creates an account with email and password. Therefore, I use
createUserWithEmailAndPassword
. Here is an example:Here are my rules in Firestore:
I tried:
.then()
) but the problem is the same than withasync/await
onAuthStateChanged()
, but the same problem occurs.request.auth.uid
), it doesn't work either.This is the first time I report an issue, so I hope I added all the necessary details.
The text was updated successfully, but these errors were encountered: