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); } u("#api-key").first().value = ""; updateAuthBtnText(); } function updateAuthBtnText() { var btn = u("#auth-btn"); if (isLoggedIn()) { btn.text("Log Out"); } else { btn.text("Log In"); } }