JSFiddle - React, Tailwind, and code Playground

by samdelagarza

JavaScript

function(window, undefined) {
    var appUrl = 'START_URL_FOR_THE_APP',
        self = this,
        login = function(){
            // will redirect to appUrl if authenticated
            self.readAuthToken();
            
            self.authenticate();
        },
        readAuthToken = function() {
            var hashPairs = window.location.hash.substring(1).split('&'),
                token = "",
                userId = "",
                redirect = false;

            if (hashPairs.length > 1) {
                token = hashPairs[0].split('access_token=')[1];
                userId = hashPairs[1].split('userid=')[1];

                if (token.length > 0) {
                    self.writeAuthToken(token.replace("%3d%3d", "=="), userId);
                    window.location.href = appUrl;
                    redirect = true;
                }
            }
            return redirect;
        },
        writeAuthToken: function(token, userId) {
            var milliseconds = 20 /* minutes */
            * 60 * 1000,
                // convert minutes to milliseconds
                expiresOn = (new Date()).setTime((new Date().getTime() + milliseconds)),
                value = JSON.stringify({
                    timestamp: expiresOn,
                    token: token,
                    userId: userId
                });

            cookie(this.tokenKey, value, {
                expires: expiresOn,
                path: cookiePath,
                domain: cookieDomain,
                secure: cookieSecure
            });
        }, authenticate = function() {
            var baseUrl = 'https://api.qa.tradestation.com/v2/authorize/?',
                clientId = 'GIVEN_BY_TRADESTATION',
                responseType = 'token',
                redirectUrl = 'DOMAIN_FOR_CLIENT',
                tradeStationAuthUrl = baseUrl + 'client_id=' + clientId + '&response_type=' + responseType + '&redirect_url' + redirectUrl;


            window.location.href =...