I am using following solution in my projects:
Define a global controller and react to the state change event:
//GLOBAL controller
angular.module("app.controllers").controller("GlobalController", ["$rootScope", "$state",
//Check state authentication
$rootScope.$on("$stateChangeStart", function(event, toState) {
if (toState.data.public === false && !($rootScope.user)) {
event.preventDefault();
$state.go("login");
}
});
}
]);
In each state, use custom data attribute data.public to prevent unauthorized access:
angular.module("app").config(["$stateProvider", function($stateProvider){
$stateProvider.state("login", {
url: "/login",
templateUrl: "loginTpl.html",
controller: "LoginCtrl",
data: {
public: true
}
});
}]);
At a successful login, set $rootScope.user to the corresponding user object!
Edit:
For your specific use case, you can use url params (checkout?sender=cart) to solve the problem. In your checkout controller use following:
if($stateParams.sender == "cart")
//ok
}else{
$state.go("cart");
}
In the button of your cart:
ui-sref="checkout({sender: 'cart'})"