I have two angular apps as part of my application. The first app is basically just the login page, and the second app is everything else. The two apps share a couple of services, including one that controls the login information/logic.
The problem occurs when a user makes use of the "remember me" functionality. We want them to be automatically logged in when they browse to the login page and redirected to the logged-in status page. The automatically logged-in bit works fine, but when we attempt to redirect the user to the status page, which is in the second app, they get stuck in an infinite loop where the login page refreshes over and over. (Interestingly, if the user is not logged in, enters the appropriate login info and clicks the
It seems that the redirect works, because the url does update correctly and the page refreshes, but the content is wrong (still the old controller and view), so it keeps re-doing the same thing-- check to see if logged in, redirect if so. How can I make sure that I actually transfer to the second app and load the right view/controller on a redirect?
Code:
twPublicApp.controller('PublicLoginCtrl', function ($scope, $http, LoginData, BrowserStorageService, $window, $location) {
window.changeApiRoot('/api/');
$scope.username = '';
$scope.password = '';
$scope.remember = false;
$scope.errorMessage = '';
$scope.authenticating = false;
if (LoginData.get('SessionToken', null, true) !== null) {
// Check if still has a valid session
$scope.authenticating = true;
LoginData.checkSession(LoginData.get('SessionToken'), LoginData.get('Location'))
.success(function(data) {
if (data.Result === true) {
console.log('User looks to be already logged in... sending on');
BrowserStorageService.changeStore('localStorage');
LoginData.set(data);
//This is the line giving me problems. I've tried these two versions, and a few others, and nothing seems to work properly.
//window.location.href = '/console#/status';
$window.location = '/console#/status';
$scope.authenticating = false;
} else {
$scope.authenticating = false;
LoginData.set({});
}
})
.error(function(jqXHR, textStatus, errorThrown) {
$scope.authenticating = false;
LoginData.set({});
});
}
$scope.authenticate = function() {
$scope.authenticating = true;
LoginData.authenticate($scope.username, $scope.password, $scope.remember)
.success(function(data) {
if (data.Result === true) {
if ($scope.remember) {
BrowserStorageService.changeStore('localStorage');
} else {
BrowserStorageService.changeStore('sessionStorage');
}
LoginData.set(data);
//Interestingly, this line works perfectly.
window.location.href = '/console#/status';
return;
}
$scope.authenticating = false;
})
.error(function(jqXHR, textStatus, errorThrown) {
$scope.authenticating = false;
});
};
});