1/* 2generalUpdates = update user icon with initials if logged in. Otherwise, replace with login button. 3 4updateViews = Ensure that All Views button appears on Inspect Page 5 6ensureBackToMain = Ensure that a button to go back to Main View appears on Inspect Page instead of All Views. Applicable for shared inspect pages. 7 8goBackToMain(o) : Redirect user to inspect page of object o with Main view / version. No action if current view is main view, and current object is o. 9 10gotoVersion(v,o): Redirect user to inspect page of object o with version v. No action if current version is v, and current object is o. 11 12isLoggedIn = Check if user is logged in, check using cookie 13 14appLoggedIn = App thinks the user is logged in, i.e., this can be implemented by maintaining the state, e.g., saving userâs initials. 15 16getUser = get id of logged in user 17 18*/ 19 20let AUTH_CACHE = {}; 21 22let PERIODIC_AUTH_SCALER = 0; 23 24/* Background Process */ 25const runBackgroundProcess = (backgroundFn,repeat=1000) => { 26 var sleep = time => new Promise(resolve => setTimeout(resolve, time)), 27 poll = (promiseFn, time) => promiseFn().then(sleep(time).then(() => poll(promiseFn, time))); 28 poll(() => new Promise(() => backgroundFn()),repeat); 29} 30 31const setupAuthFlow = (options = {}) => { 32 runBackgroundProcess(() => checkAuth(options)); 33} 34 35const startAuth = (options = {}) => { 36 /* 37 if search or landing page just updateUser 38 if inspect page, updateUser 39 */ 40 if (!options.no_user_menu) updateUser(); 41 checkAuth({ no_user_menu: options.no_user_menu }); 42 if (!options.no_periodic_check) setupAuthFlow(options); 43} 44 45 46const checkAuth = (options = {}) => { 47 let i1 = isLoggedIn(), i2 = appLoggedIn(); 48 49 // If auth change 50 if (!i1 !== !i2) { 51 52 if (!options.no_user_menu) updateUser(); 53 54 if (i1) { 55 if (isAuthModalOpen()) 56 $.modal.close(); 57 } 58 } 59 60 // If was logged in but is no longer 61 if (!i1 && AUTH_CACHE.user && options.auth_logout_callback) 62 options.auth_logout_callback(); 63 64 // If was logged in but logged into a new account 65 else if (i1 && AUTH_CACHE.user && AUTH_CACHE.user != i1 && options.auth_update_callback) 66 options.auth_update_callback(); 67 68 // If was logged out but now logged into account 69 else if (i1 && !AUTH_CACHE.user && options.auth_login_callback) 70 options.auth_login_callback(); 71 72 // No state change 73 // Runs every 10 seconds (not every second) 74 else if (options.periodic_check_callback) { 75 76 if (PERIODIC_AUTH_SCALER == 10) { 77 options.periodic_check_callback(); 78 PERIODIC_AUTH_SCALER = 0; 79 } 80 81 else PERIODIC_AUTH_SCALER++; 82 83 84 } 85 86 AUTH_CACHE.user = i1; 87} 88 89const isLoggedIn = (verify=false) => { 90 if (verify) { 91 var response = get(`${LOCAL_ENV}/neo4j_querying/permissions/logged_in`, {}); 92 return confirmResult(response); 93 } else return checkCookie(); 94} 95 96const appLoggedIn = () => { 97 return $('.account-initials').length; 98} 99 100const checkCookie = () => { 101 try { 102 const cookie = getCookie(), 103 env = getEnv(); 104 if (env in cookie) 105 return jwt_decode(cookie[env]).user || false; 106 } catch(e) {} 107 return false; 108} 109 110async function doSomethingAfterLogin(callback,options = {}) { 111 const allowclose = options?.close ?? true; 112 let logged_in = await is_logged_in(options); 113 if (logged_in === undefined) return; 114 else if (logged_in) callback(); 115 else 116 openAuthModal(allowclose, async function() { 117 logged_in = await is_logged_in(); 118 if (logged_in) callback(); 119 }); 120} 121 122const getEnv = () => { 123 if (!('env' in AUTH_CACHE && AUTH_CACHE['env'])) 124 AUTH_CACHE['env'] = confirmResult(get_raw(`${LOCAL_ENV}/neo4j_querying/permissions/getEnv`, {})); 125 return AUTH_CACHE['env']; 126} 127 128const getCookie = () => document.cookie.split(';').map(function(c) { 129 return c.trim().split('=').map(decodeURIComponent); 130}).reduce(function(a, b) { 131 try { 132 a[b[0]] = JSON.parse(b[1]); 133 } catch (e) { 134 a[b[0]] = b[1]; 135 } 136 return a; 137}, {}); 138 139 140const getUserAttribute = (attr) => { 141 142 if (AUTH_CACHE[attr]) return AUTH_CACHE[attr]; 143 144 return confirmResult(get(`${LOCAL_ENV}/neo4j_querying/user/${attr}`, {})); 145} 146 147const getUserEmail = () => getUserAttribute("email"); 148 149const getUserPhone = () => getUserAttribute("phone"); 150 151const getUserInitials = () => getUserAttribute("initials"); 152 153const getUserName = () => getUserAttribute("name"); 154 155const landing_page_new_user = () => { 156 let el = document.getElementById(`login`); 157 if (isLoggedIn(true)) { 158 const initials = getUserInitials(); 159 const dashboardURL = getLoginURL().split("/").slice(0, -1).join("/"); 160 161 let dashboardLink = (path, text) => 162 `<a class="login-link" href="${dashboardURL}/dashboard/${path}" target="_blank">${text}</a>`, 163 logoutLink = `<a class="login-link" onclick="logout();">Sign out</a>`; 164 165 dashboardLinks = [ 166 ["overview", "Account Overview"], 167 ["profile", "Profile"], 168 ["change_password", "Change Password"], 169 ["notifications", "Notifications"], 170 ]; 171 172 el.innerHTML = `
173 <span class="account-initials">${initials}</span> 174 <div class="hamburger-menu"> 175 ${dashboardLinks.map((i) => dashboardLink(...i)).join("\n")} 176 <div class="hamburger-divider"></div> 177 ${logoutLink} 178 </div> 179 `; 180 el.onclick = false; 181 182 el.classList.add("logged-in"); 183 184 initialize_hamburger(el); 185 186 } else { 187 el.classList.remove("logged-in"); 188 el.textContent = "Login"; 189 el.onclick = openAuthModal; 190 } 191}; 192 193const updateUser = () => { 194 195 const public_pages = [ 196 "automated-permitting-comparison", "landing_new", "pricing", 197 "newsletter", "contact_us", "permitconfigurator", 198 "instantpermitting", "about", "policy", "careers", "access" 199 ]; 200 if (public_pages.some(p => PAGE.includes(p))) 201 return landing_page_new_user(); 202 203 let el = document.getElementById(`login`); 204 205 if (!el) { 206 document.body.appendChild($("<div>", { 207 id: "login", 208 class: "flex-box flex-col vertically-centered", 209 })[0]); 210 el = document.getElementById(`login`); 211 } 212 213 if (isLoggedIn(true)) { 214 const initials = getUserInitials(); 215 const dashboardURL = getLoginURL().split('/').slice(0,-1).join('/'); 216 let dashboardLink = (path,text) => `<a class="login-link" href="${dashboardURL}/dashboard/${path}" target="_blank">${text}</a>`, 217 logoutLink = `<a class="login-link" onclick="logout();">Sign out</a>`; 218 219 dashboardLinks = [ 220 ["overview", "Account Overview"], 221 ["profile", "Profile"], 222 ["change_password", "Change Password"], 223 ["notifications", "Notifications"], 224 ]; 225 226 el.innerHTML = ` 227 <button class="btn-login-success gray hamburger"> 228 <span class="account-initials">${initials}</span> 229 <div class="hamburger-menu"> 230 ${dashboardLinks.map(i => dashboardLink(...i)).join("\n")} 231 <div class="hamburger-divider"></div> 232 ${logoutLink} 233 </div> 234 </button> 235 `; 236 237 initialize_hamburger(el.getElementsByClassName("hamburger")); 238 239 } else { 240 el.innerHTML = 241 `<button class="button-hoverbutton tool" onclick="openAuthModal()">Login</button>`; 242 } 243} 244 245const logout = () => { 246 post(getLogoutURL()); 247 /*TODO: post logout action*/ 248} 249 250const getLoginURL = () => { 251 if (!('login_url' in AUTH_CACHE && AUTH_CACHE['login_url'])) 252 AUTH_CACHE['login_url'] = confirmResult(get(`${LOCAL_ENV}/neo4j_querying/permissions/loginURL`, {})); 253 return AUTH_CACHE['login_url']; 254} 255 256const getLogoutURL = () => { 257 if (!('logout_url' in AUTH_CACHE && AUTH_CACHE['logout_url'])) 258 AUTH_CACHE['logout_url'] = confirmResult(get(`${LOCAL_ENV}/neo4j_querying/permissions/logoutURL`, {})); 259 return AUTH_CACHE['logout_url']; 260} 261 262function openAuthModal(close=true,afterClose=null) { 263 var opts = { 264 url: getLoginURL(), 265 autoClose: false, 266 allowToClose: close, 267 class: ['auth-modal'], 268 tags: AUTH_MODAL_TAGS, 269 }; 270 if (typeof afterClose === 'function') 271 opts.afterClose = afterClose; 272 iframeModal(opts); 273} 274 275function isAuthModalOpen() { return !!$('.auth-modal').length }
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.