-
Login using async
Example of using DataConnect with async functions that get data from the server. Each function when using await will resolve in a javascript object with the structure returned from the server in a .data object. The .result should have the string value 'ok' if all went well and 'error' or 'failed' if not. If you have error as a result, you will also have the value errorcode and error telling you what went wrong.
... <pre id="returndata" style="background-color: antiquewhite;"></pre> ... <script src="https://global.assets/js/2imply.js"></script> <script> let dataconnect = new DataConnect("data"); // set up connection to the default data object... async function dologin(){ let un = $("#username").val(); let pw = $("#password").val(); let jdata = await dataconnect.loginasync(un,pw); $("#returndata").html(JSON.stringify(jdata, null, 2)); if ('data' in jdata){ if (jdata.data.isloggedin){ $("#message").html("Logged in as user: " + jdata.data.username); const thelogindialog = document.getElementById('logindialog'); const logindialog = bootstrap.Modal.getInstance(thelogindialog); logindialog.hide(); } else $("#loginmsg").html("Wrong user name or password, try again."); } else $("#loginmsg").html("no result from server, try again."); } function showlogindialog(){ let sres = chrome.webview.hostObjects.sync.debobject.RunCommandOnWithStrRetStr("isloggedin","data",""); if (sres != "yes"){ const thelogindialog = document.getElementById('logindialog'); const logindialog = new bootstrap.Modal(thelogindialog, {}); logindialog.show(thelogindialog); } } // Startup function to run async code (async () => { try { // Test if we have a old session to use or create a new let sc = dataconnect.getsessioncookie(); if (sc == ''){ sc = localStorage.getItem('desession'); if (sc == '' || sc == null || sc == undefined){ dataconnect.generatesessioncookie(); sc = dataconnect.getsessioncookie(); if (sc != '') localStorage.setItem('desession', sc); // store the generated for next time } else // we have a existing stored so set it dataconnect.setsessioncookie(sc); } $("#message").html("Session cookie used: " + sc); let jdata = await dataconnect.checksessionasync(); $("#returndata").html(JSON.stringify(jdata, null, 2)); if (jdata.result == 'error' && jdata.errorcode == 12029){ $("#message").html("We do not have connection with data source"); } else if (jdata.result == 'ok'){ // we should have connection if ('data' in jdata && jdata.data && jdata.data.isloggedin){ $("#message").html("Aleady logged in from last time as user: " + jdata.data.username); } else showlogindialog(); } } catch (e) { console.error(''+e); } })(); </script>
As you can see this makes for a much less hard to read code when you have got over the function that starts it all. The async example will login using the async version name loginasync and it will also store and check if we are already logged in when starting up, by using localstorage in the browser to keep a copy of the session used and only show the login dialog if not already logged in.