diff options
Diffstat (limited to 'assets/js/login.js')
-rw-r--r-- | assets/js/login.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/assets/js/login.js b/assets/js/login.js new file mode 100644 index 0000000..ab59058 --- /dev/null +++ b/assets/js/login.js @@ -0,0 +1,59 @@ +// const API_ENDPOINT = "https://gimb.tk/test.php"; // deprecated +document.addEventListener("DOMContentLoaded", () => { + setupEventListeners(); +}) + +function setupEventListeners() { + // Setup login button listener + $("#login-button").click(() => { + login(); + }); + + window.addEventListener("keyup", (event) => { + // Number 13 is the "Enter" key on the keyboard + if (event.keyCode === 13) { + // Cancel the default action, if needed + event.preventDefault(); + login(); + } + }); +} + +// Handle login button click +function login() { + let username = $("#username").val(); + let password = $("#password").val(); + var gsecInstance; + try { + gsecInstance = new gsec(); + } catch (error) { + $.ajax({ + url: 'js/gsec.js?ajaxload', + async: false, + dataType: "script", + }); + try { + gsecInstance = new gsec(); + } catch (error) { + alert(D("browserNotSupported")); + } + } + gsecInstance.login(username, password).then( (value) => { + if (typeof value == "string") { + let promises_to_run = [ + localforage.setItem("logged_in", true), + localforage.setItem("username", username), + localforage.setItem("password", password) + ]; + Promise.all(promises_to_run).then(function () { + window.location.replace("/pages/timetable.html"); + }); + } else { + UIAlert("loginFailed"); + $("#password").val(""); + } + }).catch((err) => { + gsecErrorHandlerUI(err); + $("#password").val(""); + }); +} |