feathermost/model/credentials.js

39 lines
1 KiB
JavaScript

const credentials = (function() { "use strict";
const LOCALSTORAGE_KEY_SERVER = "mattermostServer";
const RE_SERVER_ITEM = new RegExp(`^${LOCALSTORAGE_KEY_SERVER}_(.*)\$`, "");
function key_for(endpoint) {
return `${LOCALSTORAGE_KEY_SERVER}_${endpoint}`;
};
return {
getServers() {
let servers = [];
for (var i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
const matches = key.match(RE_SERVER_ITEM);
if (matches) {
const endpoint = matches[1];
console.debug(`Found logged in endpoint ${endpoint}`);
let stored = JSON.parse(window.localStorage.getItem(key_for(endpoint)));
servers.push({...stored, endpoint});
}
}
return servers;
},
clear(endpoint) {
window.localStorage.removeItem(key_for(endpoint));
},
store(endpoint, login_id, token) {
window.localStorage.setItem(key_for(endpoint), JSON.stringify({login_id, token}));
},
get(endpoint) {
return JSON.parse(window.localStorage.getItem(key_for(endpoint)) || "null");
}
};
})();