-
Notifications
You must be signed in to change notification settings - Fork 155
/
dataservice.js
79 lines (69 loc) · 2.39 KB
/
dataservice.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
angular.module('utils.xhr', [])
.service('dataService', function($http, $localStorage, $sessionStorage, GLOBALS) {
var apiURL = GLOBALS.api_url;
var sessStorage = $sessionStorage;
var storage = $localStorage;
var sessionLock = false;
this.getData = function(url, successFn, errorFn) {
this.xhr('GET', url, {}, successFn, errorFn);
}
this.postData = function(url, params, successFn, errorFn) {
this.xhr('POST', url, params, successFn, errorFn);
}
this.putData = function(url, params, successFn, errorFn) {
this.xhr('PUT', url, params, successFn, errorFn);
}
this.deleteData = function(url, params, successFn, errorFn) {
this.xhr('DELETE', url, params, successFn, errorFn);
}
this.xhr = function (type, url, params, successFn, errorFn) {
$http({
method: type,
url: apiURL + url,
data: params,
headers: this.getRequestHeaders()
}).then(function successCallback(response) {
successFn(response.data);
}, function errorCallback(response) {
if (errorFn && response != undefined) errorFn(response); else console.log("Network Error", response);
}).$promise;
}
this.setAuthToken = function(token) {
sessStorage.token = token.msg;
storage.authToken = (token.remember) ? token.msg : false; // remember me
this.validateSession();
}
this.getRequestHeaders = function() {
this.validateSession();
return { 'x-access-token': (sessStorage.token) ? sessStorage.token : "" };
}
this.isLoggedIn = function() {
return sessStorage.token || storage.authToken;
}
this.validateSession = function () {
if (storage.authToken !== undefined){
sessionLock = true;
if (storage.authToken) {
$http.defaults.headers.common['x-access-token'] = storage.authToken;
sessStorage.token = storage.authToken;
}
} else if (sessionLock) {
// logout if, logout detected on another browser session
this.logout();
sessionLock=false;
}
}
this.logout = function() {
// invalidate existing token
$http.get(apiURL+"/authed/tokenRefresh")
.then(function (data) {
/* Do nothing */
}, function (err) {
console.log("debug", err);
});
delete storage.authToken;
delete sessStorage.authToken;
delete sessStorage.token;
// invalidate token on server todo
}
})