Não estou conseguinco atualizar o token do Keycloak antes da requisição.
const store = useUsuarioStore();
const url = import.meta.env.VITE_APP_KEYCLOAK_URL;
const keycloak = new Keycloak({
url,
realm: "ecosistema",
clientId: "app-vue",
});
keycloak.init({ onLoad: "login-required" }).then((auth) => {
if (!auth) {
window.location.reload();
} else {
console.log("Authenticated");
}
keycloak.loadUserProfile().then((profile) => {
console.log(`profile is ${JSON.stringify(profile) as string}`);
localStorage.setItem("token", keycloak.token as string);
localStorage.setItem("refreshToken", keycloak.refreshToken as string);
localStorage.setItem("user", keycloak.refreshToken as string);
localStorage.setItem("profile", JSON.stringify(profile));
store.fetchUsuarioLogado();
});
setInterval(() => {
keycloak
.updateToken(30)
.then((refreshed) => {
if (refreshed) {
console.log("Token refreshed" + refreshed);
} else {
let tokenParsed: number = 0;
let timeSkew: any = 0;
if (keycloak) {
if (keycloak?.tokenParsed?.exp) {
tokenParsed = keycloak?.tokenParsed?.exp;
}
if (keycloak?.tokenParsed?.exp) {
timeSkew = keycloak?.timeSkew;
}
}
console.log(
"Token not refreshed, valid for " +
Math.round(tokenParsed + timeSkew - new Date().getTime() / 1000) +
" seconds"
);
}
})
.catch(() => {
console.log("Failed to refresh token");
});
}, 60000);
});
Como ajustar ?