JavaScript
- 1 ответ
- 0 вопросов
1
Вклад в тег
import _ from 'lodash';
const mainFetch = axios.create();
export const issueToken = () => {
return new Promise((resolve, reject) => {
return keycloak.updateToken(30000).success((refreshed) => {
if (refreshed) {
resolve(keycloak.token);
sessionStorage.setItem("kctoken", `${keycloak.token}`);
} else {
console.log('not refreshed ' + new Date());
}
}).error(() => {
reject();
keycloakLogout();
});
});
}
mainFetch.interceptors.request.use((config) => {
let originalRequest = config;
if (keycloak.isTokenExpired(30000)) {
return issueToken().then((token) => {
_.set(originalRequest, 'headers.Authorization', `Bearer ${token}`);
return Promise.resolve(originalRequest);
});
} else {
_.set(config, 'headers.Authorization', `Bearer ${sessionStorage.getItem("kctoken")}`);
}
return config;
}, (err) => {
return Promise.reject(err);
});