SHARE
TWEET

Untitled

a guest Aug 26th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var jwt = (function (log, storage) {
  2.  
  3.     var TOKEN = 'auth_token';
  4.     var EXPIRATION = 'auth_expiration';
  5.  
  6.     var get = function () {
  7.  
  8.         log.debug({ class: 'jwt', method: 'get' });
  9.  
  10.  
  11.         var token = storage.get(TOKEN);
  12.         var expiration = storage.get(EXPIRATION);
  13.  
  14.         return {
  15.             token: token,
  16.             expiration: expiration
  17.         };
  18.     };
  19.  
  20.     var set = function (auth) {
  21.  
  22.         log.debug({ class: 'jwt', method: 'set', token: auth });
  23.  
  24.         storage.set(TOKEN, auth.token);
  25.         storage.set(EXPIRATION, auth.expiration);
  26.  
  27.     };
  28.  
  29.     var clear = function () {
  30.  
  31.         log.debug({ class: 'jwt', method: 'clear' });
  32.  
  33.         storage.remove(TOKEN);
  34.         storage.remove(EXPIRATION);
  35.  
  36.     };
  37.  
  38.     var intercept = function () {
  39.  
  40.         $.ajaxSetup({
  41.             beforeSend: function (xhr) {
  42.                 var auth = get();
  43.                 xhr.setRequestHeader('Authorization', auth.token);
  44.             }
  45.         });
  46.     };
  47.  
  48.     var isExpired = function () {
  49.  
  50.         var auth = get();
  51.         var expiration = new Date(auth.expiration);
  52.         var current = new Date();
  53.  
  54.         return expiration >= current;
  55.  
  56.     };
  57.  
  58.     var isAuthenticated = function () {
  59.  
  60.         var auth = get();
  61.  
  62.         return (auth !== undefined && auth !== null);
  63.  
  64.     };
  65.  
  66.     return {
  67.         get: get,
  68.         set: set,
  69.         clear: clear,
  70.         intercept: intercept,
  71.         isExpired: isExpired,
  72.         isAuthenticated: isAuthenticated
  73.     };
  74.  
  75. })(log, storage);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top