I am new to firebase and am trying to save the user object (users email) to the database through the .then() on the firebase.auth().createUserWithEmailAndPassword(email, password)
When I try to do this it throws an error:
FIREBASE WARNING: set at /users/-KlMikr6xZnfYC6dFJ6L failed: permission_denied
Basically saying I don't have permission to set the user data to the user node.
I know the function works when, because when I set the security write rules to true the userobj is created in the database.
Not sure what I am doing wrong.
controller.js
angular.module('app')
.controller('appCtrl', function($scope){
//empty user object to store info
$scope.user = {};
//empty object to accept user password
$scope.password = {};
// add login 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');
}
});
})
Firebase database rules
"rules": {
"users": {
"$uid": {
".read": true,
".write": "(auth.uid === $uid)"
}
}
}