github.com/wtsi-ssg/wrstat/v3@v3.2.3/server/static/tree/js/main.js (about)

     1  require.config({
     2      paths: {
     3          d3: "d3.v3.min",
     4          queue: "queue.v1",
     5          lodash: "lodash.min",
     6          jquery: "jquery-3.6.0.min",
     7          cookie: "js.cookie.min",
     8          flexdatalist: "jquery.flexdatalist.min",
     9          timeago: "jquery.timeago"
    10      }
    11  });
    12  
    13  requirejs.config({
    14      shim: {
    15          'flexdatalist': {
    16              deps: ['jquery']
    17          },
    18          'timeago': {
    19              deps: ['jquery']
    20          },
    21      }
    22  });
    23  
    24  requirejs(['jquery', 'cookie', 'flexdatalist', 'timeago'], function ($, cookie) {
    25      function showMap(jwt) {
    26          let user = "";
    27  
    28          try {
    29              user = getUsernameFromJWT(jwt);
    30          }
    31          catch {
    32              logout();
    33          }
    34  
    35          if (!user) {
    36              logout();
    37          }
    38  
    39          $("#login").hide()
    40          $("#body").show()
    41          $("#username").text(user);
    42  
    43          require(["dtreemap"], function () { });
    44      }
    45  
    46      $(document).ready(function () {
    47          jwt = cookie.get('jwt')
    48  
    49          if (jwt && jwt.length !== 0) {
    50              showMap(jwt)
    51              return
    52          }
    53  
    54          let oidc = cookie.get("okta-hosted-login-session-store");
    55          if (oidc && oidc.length !== 0) {
    56              fetch("/rest/v1/jwt", {
    57                  method: "POST"
    58              })
    59                  .then(d => d.json())
    60                  .then((token) => {
    61                      cookie.set("jwt", token, { path: "", secure: true, sameSite: "strict" });
    62                      showMap(token);
    63                  })
    64          } else {
    65              $("#login").show()
    66          }
    67      });
    68  
    69      $("#loginForm").submit(function (event) {
    70          event.preventDefault();
    71  
    72          var posting = $.post("/rest/v1/jwt", $("#loginForm").serialize(), "json");
    73  
    74          posting.done(function (data) {
    75              cookie.set('jwt', data, { path: '', secure: true, sameSite: 'strict' })
    76  
    77              showMap(data)
    78          });
    79  
    80          posting.fail(function () {
    81              $("#loginFailure").empty().append("Incorrect username or password");
    82              $("#body").hide()
    83          });
    84      });
    85  
    86      function getUsernameFromJWT(token) {
    87          if (!token) {
    88              logout()
    89          }
    90  
    91          return JSON.parse(atob(token.split('.')[1])).Username;
    92      }
    93  
    94      function logout() {
    95          cookie.remove("jwt", { path: "" });
    96          cookie.remove("okta-hosted-login-session-store");
    97          window.location.reload();
    98      }
    99  
   100      $("#resetButton").click(() => {
   101          window.location.hash = '';
   102          window.location.reload(false);
   103      });
   104  
   105      $("#logoutButton").click(() => {
   106          logout()
   107      });
   108  });