polycule-connect/polyculeconnect/static/scripts/index.js
Melora Hugues 44c13d0185
Some checks failed
/ docker-build-only (push) Failing after 33s
/ go-test (push) Failing after 1m1s
Fix remember-me
2024-10-20 21:20:36 +02:00

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 === "/authorize" && !urlParams.has(connectorIDParam)) {
handleLoginPage();
}