Redirect to the homepage after logged in - AngularJS Ui Router

0

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);
        });
    }
} 
})
    
asked by anonymous 12.08.2017 / 13:54

1 answer

0

Hello! In the configuration is declared a state 'App' with the url "/ home", however, the controller is trying to access the state 'home', when it should be 'App'.

Try switching $state.go('home'); to $state.go('App');

    
31.01.2018 / 14:09