gcfabri
gcfabri

Reputation: 574

AngularFire authenticating with routers $digest loop error

I am working on a simple application using AngularFire and some routes are protected using ui-router example from AngularFire guide.

When the URL is empty, ui-router handles it with $urlRouterProvider.when('/', '/account');. But when the redirect URL is protected like:

.state('account', {
                    controller: 'AccountCtrl',
                    controllerAs: 'vm',
                    url: '/account',
                    templateUrl: 'app/account/account.html',
                    data: {
                        title: '- Account'
                    },
                    resolve: {
                        'currentAuth': ['Auth', function(Auth) {
                            return Auth.$requireSignIn();
                        }]
                    }
                })

the error below is thrown:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []

Please, could you help me with this annoying error?

Upvotes: 0

Views: 52

Answers (2)

Weng Hong Wong
Weng Hong Wong

Reputation: 76

Add

event.preventDefault ()

to halt initial state

if (error === "AUTH_REQUIRED") {
    //halt default event then initiate state go to new nested page
    event.preventDefault();

    // as of now, we go to login until landing template is up
    $state.go("home");
 }

Upvotes: 1

Weng Hong Wong
Weng Hong Wong

Reputation: 76

Auth.$requireSignIn will throw an error AUTH_REQUIRED. You can try to re-direct to the right state by monitoring $stateChangeError where AUTH_REQUIRED error is caught.

// for ui-router
app.run(["$rootScope", "$state", function($rootScope, $state) {
  $rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
    // We can catch the error thrown when the $requireSignIn promise is rejected
    // and redirect the user back to the home page
    if (error === "AUTH_REQUIRED") {
      $state.go("home");
    }
  });
}]);

Upvotes: 0

Related Questions