How to "block" and redirect login page when user is already logged in?

controller.js

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

.controller(‘menuCtrl’, [’$scope’, ‘$stateParams’,
function ($scope, $stateParams) {

}])

.controller(‘homeCtrl’, [’$scope’, ‘$stateParams’,
function ($scope, $stateParams) {

}])

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

$scope.loginCheck = function(data) {
    if (data.username=="admin" && data.password=="123")
  {
    intentService.setloginFlagTrue();
    errorMessageService.write("Welcome","You have log in successfully!");
    $state.go("tabsController.projectorScreen");
    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");
}

})

route.js

angular.module(‘app.routes’, [‘ionicUIRouter’])

.config(function($stateProvider, $urlRouterProvider) {

$stateProvider

  .state('tabsController', {
url: '/page1',
templateUrl: 'templates/tabsController.html',
abstract:true

})

.state(‘tabsController.home’, {
url: ‘/home’,
cache: false,
views: {
‘tab1’: {
templateUrl: ‘templates/home.html’,
controller: ‘homeCtrl’
}
}
})

.state(‘tabsController.login’, {
url: ‘/login’,
cache: false,
views: {
‘tab1’: {
templateUrl: ‘templates/login.html’,
controller: ‘loginCtrl’
}
}
})

.state(‘tabsController.projectorScreen’, {
url: ‘/projector’,
cache: false,
views: {
‘tab1’: {
templateUrl: ‘templates/projectorScreen.html’,
controller: ‘projectorScreenCtrl’
},
‘tab2’: {
templateUrl: ‘templates/projectorScreen.html’,
controller: ‘projectorScreenCtrl’
},
‘tab3’: {
templateUrl: ‘templates/projectorScreen.html’,
controller: ‘projectorScreenCtrl’
}
}
})

.state(‘tabsController.logout’, {
url: ‘/logout’,
cache: false,
views: {
‘tab3’: {
templateUrl: ‘templates/logout.html’,
controller: ‘logoutCtrl’
}
}
})

$urlRouterProvider.otherwise(’/page1/home’)

});

a simple approach is that you need to clear the history after login is success
like this
$state.go(“tabsController.home”).then(function () {
$ionicHistory.clearHistory();
});

1 Like

It works for by back button. But unfortunately I’ve a “Home page” that has a “login” button and it still link back to my login page.

then you need to hide your login button when user login using ng-hide or something