moved also refresh token to fetch

This commit is contained in:
dcore94 2022-09-15 15:38:30 +02:00
parent 7f50e2900e
commit 9411728b3a
1 changed files with 30 additions and 38 deletions

View File

@ -649,46 +649,38 @@
refreshQueue.push(promise); refreshQueue.push(promise);
if (refreshQueue.length == 1) { if (refreshQueue.length == 1) {
var req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.withCredentials = true;
params += '&client_id=' + encodeURIComponent(kc.clientId); params += '&client_id=' + encodeURIComponent(kc.clientId);
var timeLocal = new Date().getTime(); var timeLocal = new Date().getTime();
req.onreadystatechange = function () { fetch(url, {
if (req.readyState == 4) { method: "POST",
headers: {'Content-type':'application/x-www-form-urlencoded'},
body : params,
credentials: "include"
}).then(resp=>{
if(req.status == 200){ if(req.status == 200){
return req.json()
}
if (req.status == 400) {
kc.clearToken();
}
throw "Failed to refresh token"
}).then(body=>{
logInfo('[KEYCLOAK] Token refreshed'); logInfo('[KEYCLOAK] Token refreshed');
timeLocal = (timeLocal + new Date().getTime()) / 2; timeLocal = (timeLocal + new Date().getTime()) / 2;
var tokenResponse = JSON.parse(req.responseText); var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], timeLocal); setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], timeLocal);
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess(); kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) { for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setSuccess(true); p.setSuccess(true);
} }
} else { }).catch(err=>{
logWarn('[KEYCLOAK] Failed to refresh token'); logWarn('[KEYCLOAK] Failed to refresh token');
if (req.status == 400) {
kc.clearToken();
}
kc.onAuthRefreshError && kc.onAuthRefreshError(); kc.onAuthRefreshError && kc.onAuthRefreshError();
for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) { for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {
p.setError(true); p.setError(true);
} }
} })
}
};
req.send(params);
} }
} }
} }