Skip to content

Commit

Permalink
fix(client:auth): remove decorator logic
Browse files Browse the repository at this point in the history
it seems like it wasn't working, closes #1455
  • Loading branch information
Awk34 committed Dec 5, 2015
1 parent 15912c8 commit 3229acd
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions app/templates/client/components/auth(auth)/router.decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,26 @@

(function() {

function routerDecorator(<%= filters.uirouter ? '$stateProvider' : '$provide' %>) {
var authDecorator = function(<%= filters.uirouter ? 'state' : 'route' %>) {
var auth = <%= filters.uirouter ? 'state' : 'route' %>.authenticate;
if (auth) {
<%= filters.uirouter ? 'state' : 'route' %>.resolve = <%= filters.uirouter ? 'state' : 'route' %>.resolve || {};
<%= filters.uirouter ? 'state' : 'route' %>.resolve.user = function(<%= filters.uirouter ? '$state' : '$location' %>, $q, Auth) {
return Auth.getCurrentUser(true)
.then(function(user) {
if ((typeof auth !== 'string' && user._id) ||
(typeof auth === 'string' && Auth.hasRole(auth))) {
return user;
}<% if (filters.ngroute) { %>
$location.path((user._id) ? '/' : '/login');<% } if (filters.uirouter) { %>
$state.go((user._id) ? 'main' : 'login');<% } %>
return $q.reject('not authorized');
});
};
}
};<% if (filters.ngroute) { %>
$provide.decorator('$route', function($delegate) {
for (var r in $delegate.routes) {
authDecorator($delegate.routes[r]);
}
return $delegate;
});<% } if (filters.uirouter) { %>
angular.module('<%= scriptAppName %>.auth')
.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
// Redirect to login if route requires auth and the user is not logged in, or doesn't have required role
$rootScope.$on(<% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) {
if(!next.authenticate) {
return;
}

$stateProvider.decorator('authenticate', function(state) {
authDecorator(state);
return state.authenticate;
});<% } %>
}
let query = typeof next.authenticate === 'string' ? Auth.hasRole : Auth.isLoggedIn;

angular.module('<%= scriptAppName %>.auth')
.config(routerDecorator);
query(1,2).then(good => {
if(!good) {
event.preventDefault();
Auth.isLoggedIn().then(is => {<% if (filters.ngroute) { %>
$location.path(is ? '/' : '/login');<% } if (filters.uirouter) { %>
$state.go(is ? 'main' : 'login');<% } %>
});
}
});
});
});

})();

0 comments on commit 3229acd

Please sign in to comment.