Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function loadRemote() {
- loginCheck();
- $.ajax({
- url: apiUrl + "/GroupSessions",
- data: { archived: true, from: $("#from-date").val(), to: $("#to-date").val(), minDuration: 0 },
- dataType: "json",
- beforeSend: function(xhr) {
- xhr.setRequestHeader("Authorization", "Bearer " + getCookie("token"));
- }
- }).done(function(data) {
- // (•ิ_•ิ)?
- var acc = data.reduce((acc, val) => acc.set(val.author, 1 + (acc.get(val.author) || 0)), new Map());
- $("#gamedata > tbody").empty();
- acc.forEach(function(value, key) {
- $("#gamedata > tbody:last-child").append("<tr><td>" + key + "</td><td>" + value + "</td></tr>");
- });
- //for (const [key, value] of Object.entries(acc)) {
- console.log(acc);
- })
- .fail(function() {
- alert("An error.")
- console.log("error");
- })
- .always(function() {
- console.log("complete");
- });
- }
- function setCookie(cname, cvalue, exdays) {
- var d = new Date();
- d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
- var expires = "expires=" + d.toUTCString();
- document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
- }
- function getCookie(cname) {
- console.log(`getCookie(cname: ${cname})`);
- var name = cname + "=";
- var ca = document.cookie.split(';');
- for (var i = 0; i < ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0) == ' ') {
- c = c.substring(1);
- }
- if (c.indexOf(name) == 0) {
- return c.substring(name.length, c.length);
- }
- }
- return "";
- }
- function login() {
- console.log("attempting to log in")
- var req = $.ajax({
- url: apiUrl + "/Account/Login",
- type: "POST",
- data: JSON.stringify(appLogin),
- contentType: 'application/json; charset=UTF-8',
- done: function(result) {
- console.log("Logged in.");
- setCookie("token", result, 30);
- },
- fail: function(error) {
- console.log(`Login error ${error}`);
- }
- });
- }
- function loginCheck() {
- console.log("checking login status...")
- var token = getCookie("token");
- // check for valid token
- if (token == "") {
- console.log("auth token not found or expired");
- login()
- // token found, check if is valid
- } else {
- var req = $.ajax({
- url: apiUrl + "/Evacuation",
- type: "GET",
- fail: function(error) {
- console.log(`Enable to check login due to: ${error}`);
- },
- statusCode: {
- 401: function() {
- console.log("Auth token invalid, attempting to log in...");
- login();
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment