I am currently exploring Firebase and attempting to save the user object (specifically the user's email) into the database using the .then()
method after executing
firebase.auth().createUserWithEmailAndPassword(email, password)
However, I encountered an error while trying to do this:
FIREBASE WARNING: set at /users/-KlMikr6xZnfYC6dFJ6f failed: permission_denied
This error message indicates that I do not have the necessary permissions to update the user data in the designated user node.
I have confirmed that the function works as intended. When I temporarily set the security write rules to true, the userobj
is successfully created in the database.
I am unsure what mistake I might be making in this process.
Here is a snippet from my controller.js file:
angular.module('app')
.controller('appCtrl', function($scope){
//initialize empty user object for storing information
$scope.user = {};
//initialize empty object for capturing user password
$scope.password = {};
// add sign up event
$scope.signUp = function(){
const email = $scope.user.email;
const password = $scope.password.setup;
// create new firebase user
firebase.auth().createUserWithEmailAndPassword(email, password)
// add user object to database
.then(function(){
var userObj = $scope.user;
console.log($scope.user);
// set up database references
let database = firebase.database();
let ref = database.ref();
let usersRef = ref.child('users');
// push user object to database
usersRef.push(userObj);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
};
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log('user signed in');
var uid = user.uid;
console.log(uid);
} else {
// No user is signed in.
console.log('user not signed in');
}
});
})
Below are my Firebase database rules:
"rules": {
"users": {
"$uid": {
".read": true,
".write": "(auth.uid === $uid)"
}
}
}