After login is successful, how to redirect to home page?

I have defined all the states in my app.js.

Here are app requirements

  1. always show login page when the user clicks the app.

  2. once login is successful, show home page. (shdn’t be viewing home page without login)

How I shd do this using the UI router ? what should be the default page in the UI router config function ?

.controller(‘LoginCtrl’, function($scope, $http, $log) {

$scope.onLoginButtonClick = function(){

var  postData = {
          
            "password": $scope.password,
            "username": $scope.username
          };

$http.post('http://login_URL', postData)
.success(function(data) {
    alert("SUCCESS")
    $scope.state.go('home');
})
.error(function(data) {
    alert("ERROR");
});   

}

})

The above code dipicts my app flow… I am getting the login successful alert !!! after that how to redirect to home page state ?

tried $scope.state.go(‘home’); as above but didn’t work :frowning:

I use the following to redirect my sign in states:

$location.path("/home");

Would that work for you?

router default page should be “login” in this occasion. Can you show a part of your UI router config? state.go “home” should trigger fine if your routes are setup correctly

I got it working by using $state.go(‘home’)…

Also passed the $state param to loginCtrl like :
.controller(‘LoginCtrl’, function($scope, $http, $log, $state)

thanks evryone !
:smile:

I have a similar issue, I’m using the side menu with a search box, and on return I want to redirect the user the the search controller, however it just doesnt re-route.

 .state('app.search', {
      url: "/search/:term",
      views: {
        'menuContent' :{
          templateUrl: "templates/search.html",
          controller: 'search'
        }
      }
    }) 

This is my method in the controller. I’ve tried $location.path("/search/" + term); but this doesnt work along with window.location.href = /#/app/search/term

// method in controller
$scope.doSearch = function() {
     $state.go("search",{'term':'San Diego'});
    }

Any help would be awesome.
matt

Stache, try this:

// method in controller
$scope.doSearch = function() {
  $state.go("app.search",{'term':'San Diego'});
}
1 Like

What the different between $location.path("/home"); and $scope.state.go('home'); ?

Either work, however, it is $state.go, not $scope.state.go.

Best,

Hey,

I have a same problem as above. I am unable to change the state.

App.js:

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

$stateProvider

.state(‘login’, {
url: ‘/login’,
templateUrl: ‘templates/Login.html’,
controller: ‘LoginCtrl’
})

.state(‘home’, {
url: ‘/home’,
templateUrl: ‘templates/turiamHome.html’,
controller: ‘homeCtrl’
})

$urlRouterProvider.otherwise(’/login’);

});

controller.js

.controller(‘LoginCtrl’, function($scope, LoginService, $ionicPopup, $state) {
$scope.data = {};

$scope.login = function() {
    console.log("LOGIN user: " + $scope.data.username + " - PW: " + $scope.data.password);

    LoginService.loginUser($scope.data.username, $scope.data.password).success(function(data) {
    	console.log("Login Successful");
        $state.go('home');
    }).error(function(data) {
        var alertPopup = $ionicPopup.alert({
            title: 'Login failed!',
            template: 'Please check your credentials!'
        });
    });
}

})

I am able to see the login Success message but the state is not changing.

can you please help.

Thanks

$state.go(‘app.search’)

$location.path is only goes to single and simple page and $state.go is goes to side menu pages and it is very simple task

Are you define your LoginCtrl in your login.html page?? and its not $state.go(‘home’) but its $state.go(‘turiamHome’). i think it works for you