In my Angular project setup, I have a functionality where if a user visits /cloud
without being logged in (resulting in a failure of the isLoggedIn()
check), they are redirected to /login
. Conversely, if a logged-in user tries to access /login
, they get redirected back to /cloud
.
However, upon clicking the logout button to clear local storage, I encounter the following error message (although everything continues to function normally):
Error: null is not an object (evaluating 'a.$current.locals[l]')
My logs indicate that this issue occurs within the onEnter
method of the logout controller.
Having limited experience with Angular, any advice or guidance on resolving this would be greatly appreciated.
var routerApp = angular.module('myapp', ['ui.router'])
//config for state changes
.factory('Auth', function($http, $state, $q) {
var factory = { isLoggedIn: isLoggedIn };
return factory;
function isLoggedIn(token) {
return $http.post('/auth/session', {token:token});
}
})
.config(function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/cloud');
var authenticated = ['$q', 'Auth', '$rootScope', function ($q, Auth, $rootScope) {
var deferred = $q.defer();
if (typeof window.localStorage['authtoken'] === 'undefined') {
var authtoken = undefined;
} else {
var authtoken = window.localStorage['authtoken'];
}
Auth.isLoggedIn(authtoken).then(function() {
deferred.resolve();
}, function() {
deferred.reject();
});
return deferred.promise;
}];
var authGuest = ['$q', 'Auth', '$rootScope', function ($q, Auth, $rootScope) {
var deferred = $q.defer();
if (typeof window.localStorage['authtoken'] === 'undefined') {
var authtoken = undefined;
} else {
var authtoken = window.localStorage['authtoken'];
}
Auth.isLoggedIn(authtoken).then(function() {
deferred.reject();
}, function() {
deferred.resolve();
});
return deferred.promise;
}];
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'pages/templates/login.html',
resolve: { authenticated: authGuest }
})
.state('logout', { url: '/logout', onEnter: function($state) { localStorage.clear(); $state.go('login'); } })
.state('cloud', {
url: '/cloud',
templateUrl: 'pages/templates/cloud.html',
resolve: { authenticated: authenticated }
})
})
.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeError', function (event, from, error) {
if(from.name == "login") {
$state.go('cloud');
} else {
$state.go('login');
}
});
});