var app = angular.module('DemoApp', ['ui.router', 'satellizer']);

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

  $stateProvider
    .state('home', {
      url: '/home',
      templateUrl: 'partials/home.tpl.html'
    })
    .state('secret', {
      url: '/secret',
      templateUrl: 'partials/secret.tpl.html',
      controller: 'SecretCtrl'
    })
    .state('login', {
      url: '/login',
      templateUrl: 'partials/login.tpl.html',
      controller: 'LoginSignupCtrl'
    });

  $urlRouterProvider.otherwise('/home');

});


app.controller('LoginSignupCtrl', function ($scope, $auth, $state) {

  $scope.signUp = function () {
    $auth
      .signup({email: $scope.email, password: $scope.password})
      .then(function (response) {
        $auth.setToken(response);
        $state.go('secret');
      })
      .catch(function (response) {
        console.log("error response", response);
      })
  };

  $scope.login = function () {
    $auth
      .login({email: $scope.email, password: $scope.password})
      .then(function (response) {
        $auth.setToken(response);
        $state.go('secret');
      })
      .catch(function (response) {
        console.log("error response", response);
      })
  };
});

app.controller('SecretCtrl', function ($scope, $state, $auth) {
  $scope.logout = function () {
    $auth.logout();
    $state.go("home");
  };
});
@@ -347,24 +347,59 @@
 'SecretCtrl'
+,%0A      data: %7BrequiredLogin: true%7D
 %0A    %7D)%0A    
@@ -575,16 +575,387 @@
 ;%0A%0A%7D);%0A%0A
+app.run(function ($rootScope, $state, $auth) %7B%0A  $rootScope.$on('$stateChangeStart',%0A    function (event, toState) %7B%0A      var requiredLogin = false;%0A      if (toState.data && toState.data.requiredLogin)%0A        requiredLogin = true;%0A%0A      if (requiredLogin && !$auth.isAuthenticated()) %7B%0A        event.preventDefault();%0A        $state.go('login');%0A      %7D%0A    %7D);%0A%7D);%0A%0A
 %0Aapp.con

handle requiredLogin in $stateChangeStart