All files / src/utils jwtUtils.js

0% Statements 0/40
0% Branches 0/8
0% Functions 0/14
0% Lines 0/39
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
import Cookies from 'universal-cookie';
import APIHelper from '../helper/apihelper';
import {
    DOMAIN_NAME,
    MINUS_TOKEN_TIME,
    SECURE_COOKIES,
    SAME_SITE,
    HTTP_ONLY
} from '../helper/constants';
 
const apiHelperInstance = new APIHelper();
 
let flag = 0;
/* ================================================================
 -----------SET COOKIES WITH COOKIE NAME AND MAX-AGE--------------
================================================================ */
export const setJwt = (jwt, duration) => {
    setCookies('interval', jwt, duration * 60 - MINUS_TOKEN_TIME);
    setCookies('jwt-token', jwt, duration * 60);
};
 
/* ================================================================
 -----------GET COOKIE USING COOKIE NAME-------------------------
================================================================ */
export const getJwt = () => {
    if (!getCookies('interval')) {
        if (getCookies('jwt-token')) renewToken();
    }
 
    return getCookies('jwt-token');
};
 
/* ================================================================
 -----------REMOVE COOKIE USING COOKIE NAME-------------------------
================================================================ */
export const removeJwt = () => {
    localStorage.clear();
    setCookies('interval', '', 1);
    return setCookies('jwt-token', '', 1);
};
 
/* ================================================================
 -----------SET COOKIES AS SECURE-------------------------
================================================================ */
export const isSecure = () => {
    return SECURE_COOKIES;
};
 
/* ================================================================
 -----------SET COOKIES AS ONLY SPECIFIC DOMAIN-------------------
================================================================ */
export const getdomain = () => {
    return DOMAIN_NAME;
};
 
/* ================================================================
 -----------SET COOKIES AS ONLY ACCESS FOR SAME SITE--------------
================================================================ */
export const getSameSite = () => {
    return SAME_SITE;
};
 
/* ================================================================
 -----------SET COOKIES AS HTTP ONLY--------------
================================================================ */
export const getHttpOnly = () => {
    return HTTP_ONLY;
};
 
/* ================================================================
 -----------PRIVATE ::::SET COOKIES ------------------------------
================================================================ */
function setCookies(name, value, timespan) {
    const cookies = new Cookies();
    cookies.set(name, value, {
        path: '/',
        secure: parseInt(isSecure()),
        maxAge: timespan,
        sameSite: getSameSite(),
        domain: getdomain(),
        httpOnly: parseInt(getHttpOnly())
    });
}
 
/* ================================================================
 -----------PRIVATE ::::GET COOKIES ------------------------------
================================================================ */
function getCookies(name) {
    const cookies = new Cookies();
    return cookies.get(name);
}
 
/* ================================================================
 -----------PRIVATE ::::REMOVE COOKIES ------------------------------
================================================================ */
// eslint-disable-next-line no-unused-vars
function removeCookies(name) {
    const cookies = new Cookies();
    return cookies.remove(name);
}
 
/* ================================================================
 --PRIVATE ::::THIS FUNCTION USED TO RENEW TOKEN-------------------
================================================================ */
function renewToken() {
    if (flag === 0) {
        flag = 1;
 
        fetch(apiHelperInstance.Resources.RenewToken, {
            headers: {
                Authorization: getCookies('jwt-token'),
                'Content-Type': 'application/json',
                Resource: 'defaultread',
                Action: 'View'
            }
        })
            .then(res => res.json())
            .then(data => {
                if (data.statusCode === 1) {
                    setJwt(data.data.token, data.data.duration);
                }
                flag = 0;
            })
            .catch(e => {
                flag = 1;
                window.location.href = '/';
            });
    }
}