juggl/juggl-server/js/auth.js
2020-11-08 15:21:51 +01:00

58 lines
1.1 KiB
JavaScript

function logIn(apiKey, userId) {
localStorage.apiKey = apiKey;
localStorage.userId = userId;
setState(States.IDLE);
}
function logOut() {
delete localStorage.apiKey;
delete localStorage.userId;
setState(States.PUBLIC);
}
function isLoggedIn() {
return !!localStorage.apiKey && !!localStorage.userId;
}
function getApiKey() {
return localStorage.apiKey;
}
function getUserId() {
return localStorage.userId;
}
function handleAuthBtn() {
if (isLoggedIn()) {
logOut();
} else {
var apiKey = u("#api-key").first().value;
if (apiKey === undefined || apiKey === "") {
return;
}
var userId = u("#user-id").first().value;
if (userId === undefined || userId === "") {
return;
}
logIn(apiKey, userId);
onLogIn();
}
u("#api-key").first().value = "";
updateAuthBtnText();
}
function updateAuthBtnText() {
var btn = u("#auth-btn");
if (isLoggedIn()) {
btn.text("Log Out");
} else {
btn.text("Log In");
}
}
function onLogIn() {
loadProjectList();
}