I have two separate angular applications within my project. The first application serves as the login page, while the second one contains all other functionalities. Both apps share a few services, including one that manages the login logic and information.
The issue arises when a user utilizes the "remember me" feature. We aim to automatically log in the user when they visit the login page and redirect them to the logged-in status page. Although the auto-login function works correctly, the problem occurs during the redirection to the status page in the second app. Users find themselves trapped in an infinite loop where the login page keeps refreshing continuously. (Interestingly, if the user is not logged in and enters the correct login credentials and clicks on the)
It appears that the redirect process functions since the URL updates correctly and the page refreshes. However, the content displayed remains incorrect (still displaying the old controller and view), leading to repeated actions - checking for login status, redirecting if logged in. How can I ensure a successful transfer to the second app with the right view/controller upon redirection?
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;
});
};
});