From a62819260d9c0d9bc0292f19be896c2c9b5ab422 Mon Sep 17 00:00:00 2001 From: rstular Date: Wed, 29 Jan 2020 12:49:35 +0100 Subject: Initial commit --- js/login.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 js/login.js (limited to 'js/login.js') diff --git a/js/login.js b/js/login.js new file mode 100644 index 0000000..4cbd57c --- /dev/null +++ b/js/login.js @@ -0,0 +1,61 @@ +const API_ENDPOINT = "https://gimb.tk/test.php"; +// const API_ENDPOINT = "http://localhost:5000/test.php"; + +document.addEventListener("DOMContentLoaded", () => { + setupEventListeners(); +}) + +function setupEventListeners() { + // Setup login button listener + $("#login-button").click(function () { + login(); + }); +} + +// Handle login button click +function login() { + // Get text input values + let username = $("#username").val(); + let password = $("#password").val(); + + // Make a request + $.ajax({ + url: API_ENDPOINT, + crossDomain: true, + + data: { + "u": username, + "p": password, + "m": "fetchprofil" + }, + dataType: "json", + + cache: false, + type: "GET", + + success: function (data) { + + // If ime is null, the password was incorrect + if (data["ime"] === null) { + M.toast({ html: "Login failed!" }); + $("#password").val(""); + } else { + + 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/teachers.html"); + }); + + } + }, + + error: function () { + M.toast({ html: "No internet connection!" }); + } + + }) +} \ No newline at end of file -- cgit v1.2.3