Skip to content

Commit 3ae5dd5

Browse files
committed
refactor auth interceptor and use it in config
1 parent b405b7b commit 3ae5dd5

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

public/index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script src="node_modules/angularjs-color-picker/dist/angularjs-color-picker.min.js"></script>
3737
<!-- main app file -->
3838
<script src="js/app.js"></script>
39-
<!-- constance -->
39+
<!-- http config -->
4040
<script src="js/httpConfig.js"></script>
4141
<!-- controllers -->
4242
<script src="js/controllers/mainCtrl.js"></script>
@@ -46,6 +46,8 @@
4646
<script src="js/services/colorChangerService.js"></script>
4747
<script src="js/services/stateService.js"></script>
4848
<script src="js/services/registerService.js"></script>
49+
<script src="js/services/authInterceptorService.js"></script>
50+
<script src="js/services/authService.js"></script>
4951
<!-- directives -->
5052
<script src="js/directives/topbar.js"></script>
5153
<script src="js/directives/modalOverlay.js"></script>

public/js/httpConfig.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
baseUrl: 'http://localhost:3000/api'
66
};
77

8+
Configure.$inject = ['$httpProvider'];
9+
10+
function Configure($httpProvider) {
11+
$httpProvider.interceptors.push('authInterceptorService');
12+
}
13+
814
angular
915
.module('app')
10-
.constant('httpConfig', Constant);
16+
.constant('httpConfig', Constant)
17+
.config(Configure);
18+
1119
})();

public/js/services/authInterceptorService.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,27 @@
55
.module('app')
66
.factory('authInterceptorService', Factory);
77

8-
Factory.$inject = ['$q', '$injector', '$localForage'];
8+
Factory.$inject = ['$q', '$localForage'];
99

10-
function Factory ($q, $injector, $localForage) {
10+
function Factory ($q, $localForage) {
1111
return {
12-
request: function(config) {
12+
request: config => {
1313

1414
config.headers = config.headers || {};
15-
let deferred = $q.defer();
16-
17-
if(config.url === 'https://api.cloudinary.com/v1_1/ingametrade/upload') {
18-
deferred.resolve(config);
19-
}
15+
const deferred = $q.defer();
2016

2117
if(config.url.indexOf('api') !== -1) {
2218
$localForage.getItem('authorization')
23-
.then(function (authData) {
24-
if (authData) {
19+
.then(authData => {
20+
if(authData) {
2521
config.headers.Authorization = authData.token;
2622
deferred.resolve(config);
2723
} else {
2824
console.log('there is no token yet');
2925
deferred.resolve(config);
3026
}
31-
}, function () {
32-
console.log("error with getting authorization localForage in interceptor");
33-
deferred.resolve(config);
34-
}
35-
);
27+
})
28+
.catch(() => { deferred.resolve(config) });
3629
} else {
3730
deferred.resolve(config);
3831
}

0 commit comments

Comments
 (0)