I'm trying to implement a user authentication system. I've read about 401 http status code and HTTP Auth Interceptor Module.
I've downloaded the module with bower and loaded it in my app module. I'm not using it yet but I want to use the same event that this module trigger to display the user login popup when the user open the application in the browser.
All my controller are children of the CoreController. The CoreController resolve the current user. When the application load there is not user logged in so I want to display the login form emitting the event: event:auth-loginRequired (same as http-auth-interceptor).
In this CoreController I have another rule which is listening for this specific event (event:auth-loginRequired). If this event is detected, the controller display the popup calling its state id.
The issue I have is that the console is saying that I've emitted the event correctly, but my listener is not triggered so the login popup is not showing.
Here is my CoreController:
/**
* Index controller definition
*
* @scope Controllers
*/
define(['./../module', 'moment'], function (controllers, moment) {
'use strict';
controllers.controller('CoreController', ['$scope', '$rootScope', '$state', 'user', function ($scope, $rootScope, $state, user)
{
console.log('Core Controller Loaded');
$scope.loader = true;
$scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams)
{
$scope.loader = true;
});
$scope.$on('event:auth-loginRequired', function(event, toState, toParams, fromState, fromParams)
{
console.log('catch auth required');
$state.go("main.login");
});
if (user.isLogged == false) {
console.log('emit auth required');
$rootScope.$emit('event:auth-loginRequired');
}
}]);
});
What am I doing wrong?
Cheers, Maxime
$rootScope.$on('$stateChangeStart',...)or broadcast event:$rootScope.$broadcastinstead of$rootScope.$emit$rootScopeitself can actually listen on it. This is the best way (i.e. use$rootScopeexclusively) for simple message bus pattern.