-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
107 lines (90 loc) · 3.24 KB
/
connection.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function getHost(){
var urlparts = window.location.href.split('/');
var host = urlparts[0] + '//' + urlparts[2] + '/api/';
// The following line is only needed for local testing
host = host.replace("8000/api", "5000");
return host;
}
// Manage refresh token
function setRefreshToken(refreshToken){
localStorage.setItem("refresh_token_timestamp", Date.now());
localStorage.setItem("refresh_token", refreshToken);
}
function getRefreshToken(){
var refreshToken = localStorage.getItem("refresh_token");
var timestamp = localStorage.getItem("refresh_token_timestamp");
if (timestamp===null || refreshToken===null){
location.href = "login.html";
} else {
var current = Date.now();
var dayDifference = (current-timestamp)/1000/60/60/24;
if (dayDifference > 29){
location.href = "sessionexpired.html";
} else {
return refreshToken;
}
}
}
// Manage access token
function setAccessToken(accessToken){
sessionStorage.setItem("access_token_timestamp", Date.now());
sessionStorage.setItem("access_token", accessToken);
}
function accessTokenValid(accessToken){
var timestamp = sessionStorage.getItem("access_token_timestamp");
if (timestamp===null || accessToken===null){
return false;
} else {
var current = Date.now();
var minDifference = (current-timestamp)/1000/60;
return minDifference < 14;
}
}
// Requests
function sendRequestAfterRefresh(endpoint, requestType, jsonData, onSuccess, onError){
var refreshToken = getRefreshToken();
var onSuccessfulRefresh = function(data) {
accessToken = data["access_token"];
setAccessToken(accessToken)
sendAuthenticatedRequest(endpoint, requestType, jsonData, onSuccess, onError, accessToken);
}
sendAuthenticatedRequest("refresh", "POST", {}, onSuccessfulRefresh, standardErrorAlert, refreshToken);
}
function sendAuthenticatedRequest(endpoint, requestType, jsonData, onSuccess, onError, token){
$.ajax({
url : getHost() + endpoint,
type : requestType,
data : JSON.stringify(jsonData),
dataType: "json",
contentType: "application/json",
success : onSuccess,
error : function(response, error) {
if (response["status"]==500) {
onError(response, error)
} else {
authorizationErrorAlert(response, error);
}
},
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + token);
},
cached: false
});
}
function sendRequest(endpoint, requestType, jsonData, onSuccess, onError){
var accessToken = sessionStorage.getItem("access_token");
if (accessTokenValid(accessToken)){
sendAuthenticatedRequest(endpoint, requestType, jsonData, onSuccess, onError, accessToken);
} else {
sendRequestAfterRefresh(endpoint, requestType, jsonData, onSuccess, onError);
}
}
function standardErrorAlert(response, error) {
alert("Something went wrong.");
}
function authorizationErrorAlert(response, error) {
localStorage.clear();
sessionStorage.clear();
alert("Something went wrong. Try logging in again.");
location.href = "login.html";
}