JSFiddle - React, Tailwind, and code Playground

by Sam_Butler

JavaScript

/* Helper scripts */

/* Global configuration values */
window.oaktreeConfig = [];
window.oaktreeConfig.apiPath = '/v1/';
window.oaktreeConfig.apiRoot = 'https://directdata.io/oaktree-api'; //Note no trailing slash

/* Browser support */
function _support_html5_storage() {
      try {
        return 'localStorage' in window && window['localStorage'] !== null;
      } catch (e) {
        return false;
      }
}

/* Authentication */
function oaktreeAuth(user,password) {
    var postData = {
        user: user,
        password: password
    };
    var endpoint = '/auth/login';
    return $.ajax({
        url: window.oaktreeConfig.apiRoot + endpoint,
        beforeSend: function(xhr,settings) {
            xhr.endpoint = endpoint;
        },
        method: 'POST',
        data: JSON.stringify(postData)
    });
    //Use .done(), .fail(), .always() and/or .then() on the function call
}

function saveAuth(auth) {
    /* 
     * Format of auth data:
     * {
     *  "access_token":STRING token,
     *  "expires_at":INT timestamp: seconds from epoch
     *  "expires_in":INT seconds until expiry,
     *  "token_type":"basic_password" (meaning, pass the token as the password in HTTP basic auth header)
     * }
     */
    if(_support_html5_storage()) {
        localStorage.setItem('oaktreeAuth',JSON.stringify(auth));
        /* 
         * Auth data stored as a stringified JSON object in localStorage.
         * Retrieve and parse the object to use its properties.
         * 
         * Example:
         * var token = JSON.parse(localStorage.getItem('oaktreeAuth')).access_token;
         * 
         */
    } else { //localStorage is not supported, use cookies instead
        Cookies.set('oaktreeAuth',auth);
        /*
         * Format as above.
         * 
         * Here we store the auth data as a cookie using JavaScript Cookie
         * (https://github.com/js-cookie/js-cookie)
         * Retrieve using the getJSON method to use as an object.
         * 
         *...