Skip to content

Commit deed820

Browse files
committed
Authorization rewritten.
1 parent ae8a59c commit deed820

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

src/app/modules/common/common.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@
2929
};
3030
}
3131

32-
session.$inject = ['$http'];
33-
function session($http) {
32+
session.$inject = ['$http', '$q'];
33+
function session($http, $q) {
3434
var session = this;
3535

3636
this.fetchCurrentUser = function(url) {
37-
session.pending = true;
38-
return $http.get(url)
39-
.success(function(user) {
40-
session.setCurrentUser(user);
41-
session.pending = false;
37+
var userFetch;
38+
if (session.getCurrentUser()) {
39+
userFetch = $q(function(resolve) {
40+
resolve(session.getCurrentUser());
4241
});
42+
} else {
43+
userFetch = $http.get(url);
44+
}
45+
return userFetch;
4346
};
4447

4548
this.getCurrentUser = function() {
@@ -51,19 +54,27 @@
5154
};
5255
}
5356

54-
auth.$inject = ['session', '$state', '$urlRouter'];
55-
function auth(session, $state, $urlRouter) {
56-
this.stateName = 'login';
57+
auth.$inject = ['session', '$state', '$urlRouter', '$rootScope'];
58+
function auth(session, $state, $rootScope) {
59+
var auth = this;
5760

58-
this.init = function(stateName) {
59-
this.stateName = stateName;
61+
this.init = function(stateName, profileApi) {
62+
this.stateName = stateName || 'login';
63+
this.profileApi = profileApi || '/api/profile';
6064
};
6165

62-
this.checkAccess = function(event, toState) {
66+
this.checkAccess = function(event, toState, toParams) {
6367
if (!session.getCurrentUser() && !(toState.data && toState.data.noAuth)) {
6468
event.preventDefault();
65-
$state.go(this.stateName);
66-
}
69+
session.fetchCurrentUser(auth.profileApi)
70+
.success(function(user) {
71+
session.setCurrentUser(user);
72+
$state.go(toState.name, toParams);
73+
})
74+
.error(function() {
75+
$state.go(auth.stateName);
76+
});
77+
}
6778
};
6879
}
6980

src/app/modules/core/core.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
.controller('App', AppController)
77
.run(activate);
88

9-
AppController.$inject = ['config', '$scope', '$http', 'shortHistory', 'session', 'auth'];
10-
function AppController(config, $scope, $http, shortHistory, session, auth) {
9+
AppController.$inject = ['config', '$scope', '$rootScope', 'shortHistory', 'session', 'auth'];
10+
function AppController(config, $scope, $rootScope, shortHistory, session, auth) {
1111
/*jshint validthis: true */
1212
var vm = this;
1313

1414
vm.title = config.appTitle;
1515

1616
$scope.app = config;
1717

18-
$scope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
19-
auth.checkAccess(event, toState);
18+
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
19+
auth.checkAccess(event, toState, toParams);
2020
});
2121

2222
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
@@ -29,8 +29,7 @@
2929

3030
activate.$inject = ['auth', 'session'];
3131
function activate(auth, session) {
32-
auth.init('login');
33-
session.fetchCurrentUser('/api/profile');
32+
auth.init('login', '/api/profile');
3433
}
3534

3635
})();

src/app/modules/core/core.module.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313

1414
core.config(appConfig);
1515

16-
appConfig.$inject = ['$stateProvider'];
17-
function appConfig($stateProvider) {
16+
appConfig.$inject = ['$stateProvider', '$urlRouterProvider'];
17+
function appConfig($stateProvider, $urlRouterProvider) {
1818
$stateProvider
1919
.state('app', {
2020
url: '/app',
2121
abstract: true,
2222
templateUrl: 'app/modules/core/app.html'
2323
});
24+
25+
$urlRouterProvider.otherwise(function($injector) {
26+
var $state = $injector.get('$state');
27+
$state.go('app.dashboard');
28+
});
2429
}
2530
})();

src/app/modules/profile/profile.module.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
module.config(appConfig);
77

8-
appConfig.$inject = ['$stateProvider', '$urlRouterProvider'];
8+
appConfig.$inject = ['$stateProvider'];
99

10-
function appConfig($stateProvider, $urlRouterProvider) {
10+
function appConfig($stateProvider) {
1111
$stateProvider
1212
.state('login', {
1313
url: '/login',
@@ -36,7 +36,5 @@
3636
controller: 'ProfileController',
3737
controllerAs: 'vm'
3838
});
39-
40-
$urlRouterProvider.otherwise('');
4139
}
4240
})();

0 commit comments

Comments
 (0)