I am trying to create an account system that creates a new user and then sets the data of the user to Firebase Firestore.
function signUp(email, name, password, theme, privacy){
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
var userId = user.uid;
firebase.firestore().collection("Users").doc(`${userId}`).set({
name: name,
theme: theme,
privacy: privacy
}).then(() => {
console.log("Document successfully written!");
}).catch((error) => {
console.error("Error writing document: ", error);
});
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log('User did not sign up correctly');
console.log(errorCode);
console.console.log(errorMessage);
});
}
And here are my database rules
match /Users/{userId}/{documents=**} {
allow read, write: if isSignedIn() && isOwnerOfContent();
}
function isSignedIn(){
return request.auth != null;
}
function isOwnerOfContent(){
return request.auth.uid == userId;
}
Error =
{error: {code: 400, message: "USER_NOT_FOUND",…}}
error: {code: 400, message: "USER_NOT_FOUND",…}
code: 400
errors: [{message: "USER_NOT_FOUND", domain: "global", reason: "invalid"}]
0: {message: "USER_NOT_FOUND", domain: "global", reason: "invalid"}
domain: "global"
message: "USER_NOT_FOUND"
reason: "invalid"
message: "USER_NOT_FOUND
Do you see anything wrong?
I also get this error:
signUp.js:135 Error writing document: FirebaseError: Missing or insufficient permissions. at new xr (prebuilt.js:184) at prebuilt.js:10608 at pr. (prebuilt.js:10560) at Kt (eventtarget.js:351) at jt (eventtarget.js:481) at mr.sa (webchannelbasetransport.js:368) at Qe (webchannelbase.js:2219) at Ue (channelrequest.js:822) at xe.N.Ca (channelrequest.js:703) at xe.N.Xa (channelrequest.js:564)
Your isOwnerOfContent
uses userId
, but that variable is not in scope:
function isOwnerOfContent(){
return request.auth.uid == userId;
}
To make it work, either move isOwnerOfContent
inside match /Users/{userId}/{documents=**}
, or pass the userId
value along in the call:
match /Users/{userId}/{documents=**} {
allow read, write: if isSignedIn() && isOwnerOfContent(userId);
}
...
function isOwnerOfContent(userId){
return request.auth.uid == userId;
}