-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
85 lines (73 loc) · 3.65 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function(){
'use strict';
angular.module('appServices', []);
angular.module('appControllers', []);
angular.module('appDirectives', []);
angular.module('myApp', [
'appServices', 'appControllers', 'appDirectives', 'tooltips', 'ui.router', 'ui.router.state.events',
'ngDialog', 'restangular', 'jm.i18next', 'timer'
])
.run(['$rootScope', '$state', 'User', '$stateParams', '$http', '$filter', function ($rootScope, $state, User, $stateParams, $http, $filter) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
var LoginHandler = function(callback){
User.getUserForToken().then(function(response) {
return callback(false);
}).catch(function(){
return callback(true);
})
};
$rootScope.$on("$stateChangeError", function (event, toState, toParams, fromState, fromParams, error) {
if(error.detail.status === 401) { // Unauthorized
logoutUnauthorized();
}
if(error.detail.status === 400) { // Not found
if (toState.name === "detailAuction") {
message(3, $filter('i18next')("errors.auction_not_found"));
}
$state.go('empty');
}
});
$rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
if(!toState) {
return;
}
if(needRestartToken()) {
console.log("Token restarted");
User.refreshToken();
}
if(!User.isLoggedIn && toState.data && toState.data.role){
event.preventDefault();
LoginHandler(function(loginFailed){
if(loginFailed){
logoutUnauthorized();
}
else{
for (var i = 0; i < toState.data.role.length; i++) {
if(User.role == toState.data.role[i]){
transition(toState, toParams)
}
}
}
});
}
});
$state.defaultErrorHandler(function(error) {
// override native logging errors
return;
});
function transition(toState, toParams) {
$state.transitionTo(toState, toParams, {
reload: true,
inherit: false,
notify: true
});
}
function logoutUnauthorized() {
$state.go('login');
message(3, $filter('i18next')(getErrorKeyByCode({
status: 401
})));
User.logout();
}
}]);
}());