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

js cookies all in one

js cookies all in one

cookies


// http://10.1.5.202/auto-deploy-platform/publish/index.html

// 非当前 URL 的 domain / path 下, 创建的 cookie 无法写入到当前的 域名/路径下
// domain 不同,cookie 不能写入
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/; domain=cdn.xgqfrms.xyz;";
// path 不同(Ajax path),cookie 不能写入
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/deployprod/;"

// 当前 URL 的 domain & path 下, 创建的 cookie 可以写入到当前的 域名/路径下
// 默认 domain === 当前 URL 的 domain
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/;";
// 当前 URL 的 domain & path 下,cookie 可以写入
document.cookie = "token_path1=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/auto-deploy-platform/;"
// 当前 URL 的 domain & path 下,cookie 可以写入
document.cookie = "token_path2=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/auto-deploy-platform/publish;"



        function cookies() {
            var D = new Date();
            D.setDate(D.getDate() + 30);
            document.cookie = "author=xgqfrms; website=www.xgqfrms.xyz; domain=xgqfrms.xyz; secure ;expire=" + D;
            /*
            // read cookie
            var rc = document.cookie.substring(5);
            if (rc != "www.xgqfrms.xyz") {
                alert("cookie is empty!"+rc);
                var D = new Date();
                D.setDate(D.getDate()+30);
                //write cookie
                document.cookie="name=www.xgqfrms.xyz;domain=xgqfrms.xyz;HttpOnly;expire="+D;
            }else{
              alert("cookie is:"+rc);
            }
            */
        };
        cookies();

view-source:https://www.xgqfrms.xyz/


cookies & url bug / domain bug???

URL 与 请求API 路经不一致,如何写入 cookie 并且请求 API 的时候带上 cookies ?

  1. 正常都是 URL 与 请求API 路经一一对应的
// URL (domain)
https://cdn.xgqfrms.xyz/json

// API 
https://cdn.xgqfrms.xyz/json/api/data.json

image

  1. jwt 添加一个 dev / prod 字段

 // access_token=eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiQWRtaW4iLCJleHBpcmVkIjoxNTM5ODQxMDQ2NjAwLCJ1c2VyX25hbWUiOiJhZG1pbiIsImVtYWlsIjoiaG9uZ2trQGdpbGRhdGEuY29tIn0.o453AHiAlgE90YHiCAOcPDn5__EfhB7ujaGtjx_xfqg"

image



{
    "data": {
        "cookies": "jwt_access_token",
        "prod": false,
        "dev": true
    }
}



if(document.cookie !== "" && document.cookie.includes(`access_token`)) {
    let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`access_token=`, ``).split(`.`)[1])));
    // console.log(`token =`, JSON.stringify(token, null, 4));
    let {
        // role,
        user_name,
        email
    } = token;
    sessionStorage.setItem(`email`, email);
    layui.use(["element", "layer", "form"], function() {
        let element = layui.element,
            form = layui.form,
            layer = layui.layer;
        // layui
        showRoleName(user_name);
        logoutHandler();
        modifyEmail();
        modifyPassword();
    });
}  else {
    swal({
        title: "未登录用户,无权访问!",
        text: `
            请先登录后,再访问!\n
            1 秒后自动关闭.
        `,
        icon: "warning",
        className: "warning-alert-style",
        timer: 2000,
        button: {
            text: "关闭",
            value: true,
            visible: true,
            closeModal: true
        }
    });
    setTimeout(() => {
        autoRedirectToLogin();
    }, 1000);
}


posted @ 2018-09-18 16:36  xgqfrms  阅读(140)  评论(6编辑  收藏  举报