Permalink
Browse files

handle requiredLogin in $stateChangeStart

1 parent a727d62 commit 59cba640593db1b453f809e75b11aa724e072d60 @nguyenkims committed Dec 6, 2015
Showing with 16 additions and 1 deletion.
  1. +16 −1 static/main.js
View
@@ -10,7 +10,8 @@ app.config(function ($stateProvider, $urlRouterProvider) {
.state('secret', {
url: '/secret',
templateUrl: 'partials/secret.tpl.html',
- controller: 'SecretCtrl'
+ controller: 'SecretCtrl',
+ data: {requiredLogin: true}
})
.state('login', {
url: '/login',
@@ -22,6 +23,20 @@ app.config(function ($stateProvider, $urlRouterProvider) {
});
+app.run(function ($rootScope, $state, $auth) {
+ $rootScope.$on('$stateChangeStart',
+ function (event, toState) {
+ var requiredLogin = false;
+ if (toState.data && toState.data.requiredLogin)
+ requiredLogin = true;
+
+ if (requiredLogin && !$auth.isAuthenticated()) {
+ event.preventDefault();
+ $state.go('login');
+ }
+ });
+});
+
app.controller('LoginSignupCtrl', function ($scope, $auth, $state) {

0 comments on commit 59cba64

Please sign in to comment.