I'm encountering an issue after logging in, as my page is not redirecting to the main page. Upon investigation, I found that the login condition works when using $window.location
. However, I want to implement angular-route
to restrict access to the main menu for users who are not logged in. The current problem is that if I use $window.location
, users can still access the main menu without logging in. Here is an excerpt from my JavaScript file:
app.config(function($routeProvider){
$routeProvider
.when('/',{
templateUrl:'index.html'
})
.when('/mainmenu',{
resolve:{
"check":function($location,$rootScope){
if (!$rootScope.loggedIn) {
$location.path('/');
}
}
},
templateUrl:'content/mainmenu2.html'
})
.otherwise({
redirectTo:'/'
});
});
app.controller("logincont", ['$scope','$http','$window','$rootScope','$location',function($scope,$http,$window,$rootScope,$location){
$scope.login = function () {
$http.get('http://localhost:8089/MonitoringAPI/webresources/login?a='+$scope.userid+'&d='+$scope.password).then(function(response){
$scope.reslogin = response.data;
if($scope.reslogin == null)
{
alert('Login Incorrect');
}
else
{
$rootScope.loggedIn = true;
$location.path('/mainmenu');
}
});
};
}]);
Below is a snippet of my HTML code:
<form>
<h1 class="tengah"> Form Login </h1>
<div class="form-group">
<label for="inputId">User Id</label>
<input type="text" class="form-control" ng-model="userid" placeholder="userid" required>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" ng-model="pass" placeholder="Password" required>{{password}}
</div>
<button type="submit" class="btn btn-primary" ng-click="login()">
<span class="fa fa-sign-out"></span>Login</button>
</form>