Heretiiik

Untitled

Dec 11th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function loadRemote() {
  2.     loginCheck();
  3.     $.ajax({
  4.             url: apiUrl + "/GroupSessions",
  5.             data: { archived: true, from: $("#from-date").val(), to: $("#to-date").val(), minDuration: 0 },
  6.             dataType: "json",
  7.             beforeSend: function(xhr) {
  8.                 xhr.setRequestHeader("Authorization", "Bearer " + getCookie("token"));
  9.  
  10.             }
  11.         }).done(function(data) {
  12.             // (•ิ_•ิ)?
  13.             var acc = data.reduce((acc, val) => acc.set(val.author, 1 + (acc.get(val.author) || 0)), new Map());
  14.             $("#gamedata > tbody").empty();
  15.  
  16.             acc.forEach(function(value, key) {
  17.                 $("#gamedata > tbody:last-child").append("<tr><td>" + key + "</td><td>" + value + "</td></tr>");
  18.             });
  19.             //for (const [key, value] of Object.entries(acc)) {
  20.             console.log(acc);
  21.         })
  22.         .fail(function() {
  23.             alert("An error.")
  24.             console.log("error");
  25.         })
  26.         .always(function() {
  27.             console.log("complete");
  28.         });
  29. }
  30.  
  31. function setCookie(cname, cvalue, exdays) {
  32.     var d = new Date();
  33.     d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  34.     var expires = "expires=" + d.toUTCString();
  35.     document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
  36. }
  37.  
  38. function getCookie(cname) {
  39.     console.log(`getCookie(cname: ${cname})`);
  40.     var name = cname + "=";
  41.     var ca = document.cookie.split(';');
  42.     for (var i = 0; i < ca.length; i++) {
  43.         var c = ca[i];
  44.         while (c.charAt(0) == ' ') {
  45.             c = c.substring(1);
  46.         }
  47.         if (c.indexOf(name) == 0) {
  48.             return c.substring(name.length, c.length);
  49.         }
  50.     }
  51.     return "";
  52. }
  53.  
  54. function login() {
  55.     console.log("attempting to log in")
  56.     var req = $.ajax({
  57.         url: apiUrl + "/Account/Login",
  58.         type: "POST",
  59.         data: JSON.stringify(appLogin),
  60.         contentType: 'application/json; charset=UTF-8',
  61.         done: function(result) {
  62.             console.log("Logged in.");
  63.             setCookie("token", result, 30);
  64.         },
  65.         fail: function(error) {
  66.             console.log(`Login error ${error}`);
  67.         }
  68.     });
  69. }
  70.  
  71. function loginCheck() {
  72.     console.log("checking login status...")
  73.     var token = getCookie("token");
  74.     // check for valid token
  75.     if (token == "") {
  76.         console.log("auth token not found or expired");
  77.         login()
  78.             // token found, check if is valid
  79.     } else {
  80.         var req = $.ajax({
  81.             url: apiUrl + "/Evacuation",
  82.             type: "GET",
  83.             fail: function(error) {
  84.                 console.log(`Enable to check login due to: ${error}`);
  85.             },
  86.             statusCode: {
  87.                 401: function() {
  88.                     console.log("Auth token invalid, attempting to log in...");
  89.                     login();
  90.                 }
  91.             }
  92.         });
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment