AngularFireCourse icon indicating copy to clipboard operation
AngularFireCourse copied to clipboard

logout error: angular.js:13236 Error: permission_denied at /: Client doesn't have permission to access the desired data.

Open craig-o-curtis opened this issue 8 years ago • 2 comments

Getting this error when logging out. Followed the Pluralsight AngularFire lessons, but updated to AngularFire 2.0.1 and Firebase 3.0.3.

logout component

'use strict';
angular.module('fireApp').component('logout', {
    controller: function(auth, $location){
        auth.$signOut();
        $location.path('/login');
    }
});

app.js routing:

'use strict';
(function() {
    angular.module('fireApp', ['ngRoute', 'firebase'])
    .config(function($routeProvider){
        $routeProvider
            .when('/login', {
                template: '<login current-auth="$resolve.currentAuth"></login>',
                resolve: {
                    // fbConfig - link to firebase/ creates firebase
                    // auth - signin to firebase
                    currentAuth: function(fbConfig, auth){
                        return auth.$waitForSignIn();
                    }
                }
            })
            .when('/logout', {
                template: '<logout></logout>'
            })
            .when('/home', {
                template: '<home current-user="$resolve.currentUser" get-auth="$resolve.getAuth"></home>',
                resolve: {
                    // fbConfig - link to firebase/ creates firebase
                    // auth - signin to firebase
                    currentUser: function(fbConfig, auth){
                        return auth.$waitForSignIn();
                    },
                    getAuth: function(fbConfig, auth){
                        return auth.$getAuth();
                    }
                }
            })
            .otherwise('/home');
    });
})();

login component

'use strict';
angular.module('fireApp').component('login', {
    templateUrl: '/security/login.html',
    bindings: {
        currentAuth: '='
    },
    controllerAs: 'login',
    controller: function(
        fbConfig, // new fb config object {apiKey:..., authDomain:..., databaseURL:..., storageBucket} // called with firebaseInitializeApp( {configObject} );
        auth, // service tat returns  $firebaseAuth()
        rootRef, // service that returns firebase.database().ref()
        $firebaseObject, 
        $firebaseAuth, 
        $location, 
        $timeout
    ) {
        var login = this;
        // use currrentAuth
        login.loggedIn = !!this.currentAuth;

        // checks currentAuth resolved promise
        if (login.loggedIn) {
            login.redirectMessage = 'You are already signed in. You will be redirected ... ';               

            $timeout(function(){
                $location.path('/home');
            },2000);
        }

        // POINT TO CORRECT FIREBASE
        function connectFirebaseRef(){
            var ref = firebase.database().ref();
            login.data = $firebaseObject(ref);
        }

        // ANONYMOUS LOGIN!
        login.anonLogin = function() {

            login.firebaseUser = null;
            login.error = null;

            auth.$signInAnonymously().then(function(firebaseUser) {
                // login.firebaseUser = firebaseUser;

                // LISTENER to Firebase, show connected
                rootRef.on('value', function(){
                    $log.info('connected');
                });

                connectFirebaseRef();

                $location.path('/home');
            }).catch((function(error) {
                login.error = error;
            }).bind(this));
        };

        // FACEBOOK LOGIN
        login.facebookLogin = function() {
                    var provider = new firebase.auth.FacebookAuthProvider();        
                    firebase.auth().signInWithPopup(provider).then(function(firebaseUser) {
                var token = firebaseUser.credential.accessToken;
                var user = firebaseUser.user;
                                login.firebaseUser = firebaseUser;

                /*** needed to redirect ***/
                connectFirebaseRef();

                // redirect to home
                $location.path('/home');
            }).catch((function(error) {
                        login.error = error;
                        login.errorMessage = error.message;
            }).bind(this));     
        };
    }
});

home component

'use strict';
angular.module('fireApp').component('home', {
    templateUrl: '/home/home.html',
    bindings: {
        currentUser: '='
    },
    controllerAs: 'home',
    controller: function($log, fbConfig, auth, rootRef, $firebaseObject, $firebaseAuth, userService, $location){
        var home = this;

        // check if signedIn, treat as bool
            // for assigning user object
        home.signedIn = !!home.currentUser;
        if ( !home.signedIn ) {
            $location.path('/login');
        }

        console.log('still on home controller?');

        var checkUser = auth.$getAuth();
        // Complete home wrapper
        if (home.signedIn) {
            // signed in, so simply get user data here
            home.user = auth.$getAuth();        

            // ANON login
                // copy object,
            if (home.user.isAnonymous) {
                // RESET because is readonly object
                home.user = {};
                home.user.displayName = 'Anonymous';
                home.user.email = 'Anonymous';
                home.user.photoURL = '/assets/img/anonymous_user.jpg';
            }

            // Check if user logged in, then display data
            if (home.user.displayName !== undefined) {
                // LOGGED IN 
            } else if (home.user.diplayName === undefined) {
                // FORCED LOGIN
                home.user = null;
                // $location.path('/login');
            }
            // SHOW CONTENT 
              // show database
            var ref = firebase.database().ref();
              // download the data into a local object
            home.data = $firebaseObject(ref);
        }
    }
});

It looks like after logging out, the console.log('Still on the home controller?'); is fired after the error and once back on login.

Hope you can help. Been working on this 3 weeks now after work. 👍

craig-o-curtis avatar Jul 28 '16 06:07 craig-o-curtis

facing same issue solution please

chaitanyamannem avatar Sep 25 '16 13:09 chaitanyamannem

Because you don't disconnect the database

solution: import * as firebase from 'firebase/app';

// method // loggout() { firebase.database().goOffline(); }

Leviiathan avatar Apr 23 '18 06:04 Leviiathan