I am trying to prevent the user from going straight to bookmarked URL when not logged in (maybe I should figure out how to only display one URL, no matter what the state ?).
If the user has logged in, then $scope.serverToken will be set.
If not, I want to change state to index.login - BUT the login dialog is not being displayed - perhaps because of the first check in the function??
What am I doing wrong?
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams)
{
if (toState == fromState)
return;
if (fromState.name == "")
{
$scope.serverToken = "";
event.preventDefault();
$scope.SetLoginDialog('login');
$state.go("index.login");
return;
}
// Take the user to the login page if server token expired only if user is not already transitioning to the
// login page
if (($scope.serverToken == "") && (toState.name !== 'index.login'))
{
console.warn('Attempt to go directly to URL without logging in');
event.preventDefault();
$scope.SetLoginDialog('login');
$state.go("index.login");
return;
}
var url = HOST + 'api/is_user_logged_in.php?token=' + $scope.serverToken;
console.log('Check if user is logged in at ' + url);
$http.get(url)
.success(function(data, status, headers, config)
{
State changes are via a menu who's entries look like this
<li ui-sref-active="active">
<a ui-sref="index.events"><i class="fa fa-desktop"></i> <div class="nav-label">Events</div></a>
</li>
This might also help you to help me ...
$stateProvider
.state('index', {
abstract: true,
url: "/index",
templateUrl: "views/common/content.html",
})
.state('index.login', {
url: "/login",
templateUrl: "views/login.html",
data: { pageTitle: 'Login' }
})
.state('index.overview', {
url: "/overview",
templateUrl: "views/overview.html",
data: { pageTitle: 'Overview' }
})
.state('index.events', {
url: "/events",
templateUrl: "views/events.html",
data: { pageTitle: 'Events' },
})
Here's some of my controller, in case it helps ...
angular
.module('MyApp')
.controller('MainCtrl', MainCtrl)
function MainCtrl($rootScope, $scope, $http, $interval, $state, $location)
{