I'm using AngularJS 1.6.4 with ui-router and I need to redirect the user to the main.html page, in case the login is successful.
My routes are configured like this:
var app = angular.module('odontoSystem', ['ui.router']);
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('App', {
url: '/home',
templateUrl: 'main.html'
})
// nested list with custom controller
.state('App.appointments', {
url: '/appointments',
templateUrl: 'view/appointment/appointment.html',
controller: 'AppointmentController'
})
});
In my function that logs in, I use $ state.go ('home'), if the user is valid, but nothing happens, continue on the login page.
Here is my login function:
app.controller('LoginController', function($scope, $http, $location, $state) {
$scope.doLogin = function(){
if($scope.formLogin.$valid){
$http.post("rest/user/doLogin", $scope.user).then(function successCallback(response) {
console.log(response);
if(response.status === 200 && response.data.id != undefined){
localStorage.setItem('permission',
response.data.permission);
localStorage.setItem('name', response.data.name);
localStorage.setItem('mail', response.data.mail);
$state.go('home');
}
},
function errorCallback(response) {
console.log(response);
});
}
}
})