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

47 lines
1.4 KiB
JavaScript

const connectorNameKey = "connectorName";
const connectorIDParam = "connector_id";
const urlParams = new URLSearchParams(window.location.search);
function chooseConnector(connectorName) {
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);
}
}
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);
window.location.href = nextURL;
}
if (window.location.pathname === "/auth" && !urlParams.has(connectorIDParam)) {
handleLoginPage();
}