Skip to content

Commit e22e214

Browse files
committed
Improved authorization.
1 parent d404a04 commit e22e214

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/app/modules/common/common.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@
2929
};
3030
}
3131

32-
function session() {
32+
33+
session.$inject = ['$http'];
34+
function session($http) {
35+
var session = this;
36+
37+
this.fetchCurrentUser = function(url) {
38+
session.pending = true;
39+
return $http.get(url)
40+
.success(function(user) {
41+
session.setCurrentUser(user);
42+
session.pending = false;
43+
});
44+
};
45+
3346
this.getCurrentUser = function() {
3447
return this.user;
3548
};
@@ -39,20 +52,26 @@
3952
};
4053
}
4154

42-
auth.$inject = ['session', '$state'];
43-
function auth(session, $state) {
55+
auth.$inject = ['session', '$state', '$urlRouter'];
56+
function auth(session, $state, $urlRouter) {
4457
this.stateName = 'login';
4558

4659
this.init = function(stateName) {
4760
this.stateName = stateName;
4861
};
4962

5063
this.checkAccess = function(event, toState) {
64+
session.pending && _onSessionPending(event);
5165
if (!session.getCurrentUser() && !(toState.data && toState.data.noAuth)) {
5266
event.preventDefault();
5367
$state.go(this.stateName);
5468
}
5569
};
70+
71+
function _onSessionPending(event) {
72+
event.preventDefault();
73+
setTimeout($urlRouter.sync, 0);
74+
}
5675
}
5776

5877
})();

src/app/modules/core/core.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
angular
55
.module('app.core')
6-
.controller('App', AppController);
6+
.controller('App', AppController)
7+
.run(activate);
78

89
AppController.$inject = ['config', '$scope', '$http', 'shortHistory', 'session', 'auth'];
910
function AppController(config, $scope, $http, shortHistory, session, auth) {
@@ -23,16 +24,13 @@
2324
$(document).trigger('sn:loaded', [event, toState, toParams, fromState, fromParams]);
2425
});
2526

26-
activate();
27+
shortHistory.init($scope);
28+
}
2729

28-
function activate() {
29-
shortHistory.init($scope);
30-
auth.init('login');
31-
$http.get('/api/profile')
32-
.success(function(user) {
33-
session.setCurrentUser(user);
34-
});
35-
}
30+
activate.$inject = ['auth', 'session'];
31+
function activate(auth, session) {
32+
auth.init('login');
33+
session.fetchCurrentUser('/api/profile');
3634
}
3735

3836
})();

0 commit comments

Comments
 (0)