summaryrefslogtreecommitdiffstats
path: root/assets/js/login.js
blob: fc6dfcd82c50411fe1578fa639d7039aa8aa3fc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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)
            ];
            read_val(0);
            Promise.all(promises_to_run).then(function () {
                window.location.replace("/pages/timetable.html");
            });
        } else {
            UIAlert("loginFailed");
            $("#password").val("");
        }
    }).catch((err) => {
        gsecErrorHandlerUI(err);
        $("#password").val("");
    });
}