This commit is contained in:
parent
4836bd2944
commit
b613aeb9d9
2 changed files with 149 additions and 27 deletions
63
index.html
63
index.html
|
|
@ -351,7 +351,6 @@
|
|||
|
||||
if (code) {
|
||||
// Exchange code for token
|
||||
// NOTE: This assumes the Authentik client is "Public" and doesn't require a secret
|
||||
try {
|
||||
const response = await fetch("https://idm.flegr.me/application/o/token/", {
|
||||
method: "POST",
|
||||
|
|
@ -366,6 +365,9 @@
|
|||
const data = await response.json();
|
||||
if (data.access_token) {
|
||||
localStorage.setItem("access_token", data.access_token);
|
||||
if (data.refresh_token) {
|
||||
localStorage.setItem("refresh_token", data.refresh_token);
|
||||
}
|
||||
window.history.replaceState({}, document.title, window.location.pathname);
|
||||
} else {
|
||||
console.error("Token exchange failed:", data);
|
||||
|
|
@ -375,13 +377,64 @@
|
|||
}
|
||||
}
|
||||
|
||||
const storedToken = localStorage.getItem("access_token");
|
||||
if (!storedToken) {
|
||||
const url = `${AUTH_URL}?client_id=${CLIENT_ID}&response_type=code&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&scope=openid+profile+email`;
|
||||
let accessToken = localStorage.getItem("access_token");
|
||||
const refreshToken = localStorage.getItem("refresh_token");
|
||||
|
||||
// Check if token is expired or about to expire (within 60 seconds)
|
||||
if (accessToken && isTokenExpired(accessToken)) {
|
||||
console.log("Access token expired, attempting refresh...");
|
||||
if (refreshToken) {
|
||||
accessToken = await performTokenRefresh(refreshToken);
|
||||
} else {
|
||||
accessToken = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
// Redirect to Authentik with offline_access scope to get a refresh_token
|
||||
const url = `${AUTH_URL}?client_id=${CLIENT_ID}&response_type=code&redirect_uri=${encodeURIComponent(REDIRECT_URI)}&scope=openid+profile+email+offline_access`;
|
||||
window.location.href = url;
|
||||
return null;
|
||||
}
|
||||
return storedToken;
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
function isTokenExpired(token) {
|
||||
try {
|
||||
const payload = JSON.parse(atob(token.split('.')[1]));
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
return payload.exp < (now + 60);
|
||||
} catch (e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
async function performTokenRefresh(refreshToken) {
|
||||
try {
|
||||
const response = await fetch("https://idm.flegr.me/application/o/token/", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
grant_type: "refresh_token",
|
||||
client_id: CLIENT_ID,
|
||||
refresh_token: refreshToken,
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.access_token) {
|
||||
localStorage.setItem("access_token", data.access_token);
|
||||
if (data.refresh_token) {
|
||||
localStorage.setItem("refresh_token", data.refresh_token);
|
||||
}
|
||||
console.log("Token refreshed successfully");
|
||||
return data.access_token;
|
||||
}
|
||||
console.error("Token refresh failed:", data);
|
||||
return null;
|
||||
} catch (e) {
|
||||
console.error("Error during token refresh:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
(async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue