M
M
MrCroissant2015-12-19 00:42:27
Angular
MrCroissant, 2015-12-19 00:42:27

How to pass authorization uid to firebase?

I'm doing a project in Angular with firebase. And I got a problem with roles and uid.
I made an authorization page that works and after authorization I get to the page on which articles from firebase should be displayed. But it keeps telling me in the console:

Error: permission_denied: Client doesn't have permission to access the desired data.
    at Error (native)
    at J (https://cdn.firebase.com/js/client/2.2.4/firebase.js:120:36)
    at Object.J (https://cdn.firebase.com/js/client/2.2.4/firebase.js:201:378)
    at https://cdn.firebase.com/js/client/2.2.4/firebase.js:187:3
    at wh.h.Fd (https://cdn.firebase.com/js/client/2.2.4/firebase.js:191:104)
    at kh.Fd (https://cdn.firebase.com/js/client/2.2.4/firebase.js:182:364)
    at ch.wg (https://cdn.firebase.com/js/client/2.2.4/firebase.js:180:281)
    at fh (https://cdn.firebase.com/js/client/2.2.4/firebase.js:174:464)
    at WebSocket.va.onmessage (https://cdn.firebase.com/js/client/2.2.4/firebase.js:173:245)

And I can't do anything. The roles on firebase are:
{
  "rules": {
    "users": {
      ".read": "auth != null && (root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
      "$uid": {
        ".read": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
        ".write": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)"
      }
    },
    "files": {
      "$uid": {
        ".read": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
        ".write": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)"
      }
    },
    "stones": {
      "$uid": {
        ".read": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
        ".write": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)"
      }
    },
    "images": {
      "$uid": {
        ".read": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
        ".write": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)"
      }
    },
    "tags": {
      "$uid": {
        ".read": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)",
        ".write": "auth != null && (auth.uid == $uid || root.child('users').child(''+auth.uid).child('isAdmin').val() == true)"
      }
    },
    "releases": {
      ".read": true,
      ".write": true
    }
  }
}

I tried to expose to everyone:
".read": true,
      ".write": true

Everything works fine, but everything needs to be through uid.
Here is my authorization:
NameApp.controller('authorizationCtrl', ['$scope','$location','$firebaseAuth', function($scope,$location,$firebaseAuth) {
 var ref = new Firebase("https://asdasd.firebaseio.com");
    var loginObj = $firebaseAuth(ref);
  
  $scope.user = {};
  $scope.SignIn = function(e) {
    var username = $scope.user.email;
    var password = $scope.user.password;
    ref.authWithPassword({
      email    : username,
      password : password
    }, 
      function authHandler(error, authData) {
      if (error) {
        console.log("Login Failed!", error);
      } else {
        console.log("Authenticated successfully with payload:", authData);

        $scope.$apply(function() {
        $location.path('/main');
      });
      }
    }
    );

 }
}]);

And how I output everything:
angular.module('qweqw.main', ['ngRoute', 'firebase'])

.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/main', {
        templateUrl: 'views/main/main.html',
        controller: 'mainCtrl'
    });
}])

// Inject $firebaseArray instead of $firebase
.controller("mainCtrl", ["$scope", "$firebaseArray",
  
  function($scope, $firebaseArray) {
   

var ref = new Firebase('https://asdasd.firebaseio.com/users');
var users = $firebaseArray(ref);

users.$loaded()
    .then(function(){
        angular.forEach(users, function(users) {
           // console.log("asdasd" + users);
            var q = users;
            console.log(q)
        })
    });

  }
]);

I would be incredibly grateful for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question