Problem with checking if user is logged in and redirecting from login button to main page

Hi, currently I am trying to block my login page after user is logged in and now having a problem of checking whether the user is logged in or logout.

What i want to do in my app:

  1. IF user is logged in, even if user click login again, it will pop out a message saying “User has already logged in!” and redirect him to main page rather than being able to login multiple times.
    2)IF system check that no user has login yet, it will stay at login form.

controller.js

angular.module(‘app.controllers’, [])

.controller(‘menuCtrl’, [’$scope’, ‘$stateParams’, // The following is the constructor function for this page’s controller. See https://docs.angularjs.org/guide/controller
// You can include any angular dependencies as parameters for this function
// TIP: Access Route Parameters for your page via $stateParams.parameterName
function ($scope, $stateParams) {

}])

.controller(‘homeCtrl’, function($scope, $stateParams, $state, LoginService, errorMessageService, $ionicHistory, intentService, $ionicPopup) {
$scope.data = {};

$scope.login = function(data){

if (intentService.setloginFlagTrue)

{
$state.go(“tabsController.projector”).then(function () {
$ionicHistory.clearHistory();
});
errorMessageService.write("!!!",“User has already logged in!”);

}
else
{
$state.go(“tabsController.login”);
}
}

})

.controller(‘loginCtrl’, function($scope, LoginService, $ionicHistory, $ionicPopup, $state, $timeout, intentService, errorMessageService) {
$scope.data = {};

$scope.loginCheck = function(data) {
    if (data.username=="admin" && data.password=="admin")
  {
    intentService.setloginFlagTrue();
    errorMessageService.write("Welcome","You have log in successfully!");
    $state.go("tabsController.projectorScreen").then(function () {
      $ionicHistory.clearHistory();
    });
    console.log("Login success! The flag is now set to " + $scope.loginFlag);
  }
  else
  {
    intentService.setloginFlagFalse();
    errorMessageService.write("Error","You have entered invalid credentials.")
    console.log("Login failed! The flag is now set to " + $scope.loginFlag);
  }
}

})

.controller(“projectorScreenCtrl”, function($scope, $ionicPopup , $state, intentService, errorMessageService) {

$scope.showAlert = function() {
errorMessageService.write(“Unauthorised Access”,“Please login before using this option.”);
console.log("Control menu option is not allowed. The flag was set as " + intentService.getloginFlagStatus());
$state.go(“tabsController.login”);
}
})

.controller(“logoutCtrl”, function($scope, $state, $ionicPopup, intentService, errorMessageService) {

$scope.logoutYes = function() {
  intentService.setloginFlagFalse();
  errorMessageService.write("Thank you","You have logout successfully.");
  $state.go("tabsController.home");
  console.log("Logout is successful.The flag is now set as " + intentService.getloginFlagStatus());
}
$scope.logoutNo = function() {
  intentService.setloginFlagTrue();
  $state.go("tabsController.projectorScreen");
  console.log("Logout is reverted.The flag is now set as " + intentService.getloginFlagStatus());
}
$scope.showAlert = function() {
     errorMessageService.write("Unauthorised Access","Please login before using this option.")
     console.log("Logout menu option is not allowed. The flag was set as " + intentService.getloginFlagStatus());
     $state.go("tabsController.login");
}

})

1 Like