xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Cookies Reader

Cookies Reader


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 *
 * @description CookiesReader
 * @augments
 * @example
 *
 */

const CookiesReader = (key = ``, debug = false) => {
    if(key) {
        let name = `${key}=`;
        let decodedCookie = decodeURIComponent(document.cookie);
        let cookies = decodedCookie.split(`;`);
        for(let i = 0; i <cookies.length; i++) {
            let cookie = cookies[i];
            while (cookie.charAt(0) === ` `) {
                // trim() ???
                cookie = cookie.substring(1);
            }
            if (cookie.indexOf(name) === 0) {
                return cookie.substring(name.length, cookie.length);
            }
        }
        return "";
    } else {
        console.error(`Please pass the key of cookie!, key = ${key}`);
    }
};

const getCookieValue = (key = ``, debug = false) => {
    if(key) {
        let name = `${key}=`;
        let decodedCookie = decodeURIComponent(document.cookie);
        let cookies = decodedCookie.split(`;`);
        for(let i = 0; i <cookies.length; i++) {
            let cookie = cookies[i];
            while (cookie.charAt(0) === ` `) {
                // trim() ???
                cookie = cookie.substring(1);
            }
            if (cookie.indexOf(name) === 0) {
                return cookie.substring(name.length, cookie.length);
            }
        }
        return "";
    } else {
        console.error(`Please pass the key of cookie!, key = ${key}`);
    }
};



export default CookiesReader;

export {
    CookiesReader,
    getCookieValue,
};



/*


import {
    getCookieValue,
    // CookiesReader,
} from "./cookies-reader";

JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));


JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));


*/

demo


import {
    getCookieValue,
    // CookiesReader,
} from "./cookies-reader";


JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));





import {
    getCookieValue,
    // CookiesReader,
} from "./cookies-reader";

import {
    ROLE,
    DEV
} from "./url-roles";



let access_token = ``;

if (DEV === "production") {
    access_token = `access_token_prod`;
} else {
    access_token = `access_token_test`;
}


// let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`${access_token}`, ``).split(`.`)[1])));
let token = JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));


HttpOnly & bug

bug

const CookiesReader = (key = ``, debug = false) => {
    if(key) {
        let name = `${key}=`;
        let decodedCookie = decodeURIComponent(document.cookie);
        let cookies = decodedCookie.split(`;`);
        for(let i = 0; i <cookies.length; i++) {
            let cookie = cookies[i];
            while (cookie.charAt(0) === ` `) {
                // trim() ???
                cookie = cookie.substring(1);
            }
            if (cookie.indexOf(name) === 0) {
                return cookie.substring(name.length, cookie.length);
            }
        }
        return "";
    } else {
        console.error(`Please pass the key of cookie!, key = ${key}`);
    }
};

CookiesReader(`JSESSIONID`);


posted @ 2018-10-19 14:56  xgqfrms  阅读(156)  评论(7编辑  收藏  举报