polycule-connect/polyculeconnect/static/scripts/index.js

48 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const connectorNameKey = "connectorName";
const connectorIDParam = "connector_id";
2023-10-15 18:11:50 +00:00
const urlParams = new URLSearchParams(window.location.search);
2023-10-15 18:11:50 +00:00
function chooseConnector(connectorName) {
2023-10-15 18:11:50 +00:00
let nextURL = new URL(window.location.href);
nextURL.searchParams.append(connectorIDParam, connectorName);
setState(STATE_IN_PROGRESS);
window.location.href = nextURL;
}
// Clean the cache in case previous authentication didn't succeed
// in order not to get stuck in a login loop
function handleFailedState() {
if (getState() !== STATE_SUCCESS) {
localStorage.removeItem(connectorNameKey);
}
}
// Add the connector name to local storage in case the auth succeeded
// and the remember-me box was checked
function handleSuccess(connectorName) {
setState(STATE_SUCCESS);
if (localStorage.getItem(rememberMeKey)) {
localStorage.removeItem(rememberMeKey);
localStorage.setItem(connectorNameKey, connectorName);
}
}
2023-10-15 18:11:50 +00:00
function handleLoginPage() {
handleFailedState();
let connectorName = localStorage.getItem(connectorNameKey);
if (getState() === STATE_SUCCESS && connectorName != null) {
chooseConnector(connectorName);
}
}
function goBackToLogin() {
let nextURL = new URL(window.location.href);
nextURL.searchParams.delete(connectorIDParam);
2023-10-15 18:11:50 +00:00
window.location.href = nextURL;
}
2024-10-20 19:20:36 +00:00
if (window.location.pathname === "/authorize" && !urlParams.has(connectorIDParam)) {
handleLoginPage();
}