Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Filter states for which login is required during state change
Browse files Browse the repository at this point in the history
A state parameter was added for the routes that require user authentication. Now, everytime a statechange occurs, the destination state is checked and user is redirected to signin page if necessary. Note the state parameter is added within `data`, so that nested states can inherent its value.
  • Loading branch information
igorauad committed Jul 24, 2015
1 parent e3405d2 commit 9fc88e6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
10 changes: 8 additions & 2 deletions modules/articles/client/config/articles.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ angular.module('articles').config(['$stateProvider',
}).
state('articles.create', {
url: '/create',
templateUrl: 'modules/articles/views/create-article.client.view.html'
templateUrl: 'modules/articles/views/create-article.client.view.html',
data: {
requiresLogin: true
}
}).
state('articles.view', {
url: '/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html'
}).
state('articles.edit', {
url: '/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html'
templateUrl: 'modules/articles/views/edit-article.client.view.html',
data: {
requiresLogin: true
}
});
}
]);
14 changes: 14 additions & 0 deletions modules/core/client/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio
}
]);

angular.module(ApplicationConfiguration.applicationModuleName).run(function($rootScope, $state, Authentication) {
// Check authentication before changing state
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toState.data && toState.data.requiresLogin && Authentication.user === '') {
event.preventDefault();
$state.go('authentication.signin', {}, {
notify: false
}).then(function() {
$rootScope.$broadcast('$stateChangeSuccess', 'authentication.signin', {}, toState, toParams);
});
}
});
});

//Then define the init function for starting up the application
angular.element(document).ready(function() {
//Fixing facebook bug with redirect
Expand Down
5 changes: 4 additions & 1 deletion modules/users/client/config/users.client.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ angular.module('users').config(['$stateProvider',
state('settings', {
abstract: true,
url: '/settings',
templateUrl: 'modules/users/views/settings/settings.client.view.html'
templateUrl: 'modules/users/views/settings/settings.client.view.html',
data: {
requiresLogin: true
}
}).
state('settings.profile', {
url: '/profile',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
angular.module('users').controller('SettingsController', ['$scope', '$location', 'Authentication',
function($scope, $location, Authentication) {
$scope.user = Authentication.user;

// If user is not signed in then redirect back home
if (!$scope.user) $location.path('/');
}
]);

0 comments on commit 9fc88e6

Please sign in to comment.