All files / session refresh.js

32.65% Statements 16/49
100% Branches 0/0
0% Functions 0/1
32.65% Lines 16/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                   1x 1x 1x  
import cookie from 'cookie';
import axios from 'axios';
import LogoutSession from './logout.js';
 
/**
 * Makes an API request to refresh the session token cookie, if it exists.
 * @param {Object}   [options] - Optional configuration parameters.
 * @param {function} [options.onSuccess] - An optional success callback.
 * @param {function} [options.onError] - An optional error callback.
 * @param {Object} [options.window] - The DOM window.
 * @return {Function} Returns a promise
 */
const RefreshSession = (options = {}) => {
    let cookies = cookie.parse(options.window.document.cookie);

    if (cookies.hasOwnProperty('qpp_auth_token')) {
        const requestOptions = {
            method: 'POST',
            url: '/api/auth/sessions/refresh',
            headers: {
                Accept: 'application/vnd.qpp.cms.gov.v1+json',
                'Content-Type': 'application/json',
                Authorization: `Bearer ${cookies.qpp_auth_token}`
            }
        };

        let onSuccess = (/*response*/) => {
            if (typeof options.onSuccess === 'function') {
                options.onSuccess();
            }
        };

        let onError = (/*response*/) => {
            if (typeof options.onError === 'function') {
                options.onError();
            }

            return LogoutSession(options.window); // eslint-disable-line new-cap
        };

        return axios(requestOptions)
            .then(onSuccess, onError)
            .catch(function(err) {
                console.log(err);
            });
    }
};
 
export default RefreshSession;