@samuelluo/auto-refresh-token
v1.2.2
Published
a new token refresh way
Downloads
1
Readme
auto-refresh-token( 非开源!!! )
新型token refresh方法
author:罗舒能、王培树
使用方法
import { priKey, pubKey } from "../test/security.js";
import axios from "axios";
import { GeneratorSecurityTokenInstance, initSecurityKeys } from "./token.js";
// 创建请求实例
let instance = axios.create({
baseURL: "http://exam-user.dlnuxeon.cn",
timeout: 5000,
});
let instance1 = axios.create({
baseURL: "http://exam-user.dlnuxeon.cn",
timeout: 5000,
});
let instance2 = axios.create({
baseURL: "http://exam-user.dlnuxeon.cn",
timeout: 5000,
});
// 初始化RSA加密
initSecurityKeys(pubKey, priKey);
// 初始化拦截器
const handler = GeneratorSecurityTokenInstance(instance, window.localStorage);
const handler1 = GeneratorSecurityTokenInstance(instance1, window.localStorage);
const handler2 = GeneratorSecurityTokenInstance(instance2, window.localStorage);
async function test() {
// 账号密码登录
let userId = await instance
.post(
"/hello/login",
{
account: "123456",
password: "e10adc3949ba59abbe56e057f20f883e",
},
{ step: -1 }
)
.then((res) => {
console.log(res);
return res.data.userId;
});
// 页面退出
handler.unloadPage(userId);
// 自动登录
userId = await instance1.post("hello/login", {}, { step: 0 }).then((res) => {
console.log(res);
return res.data.userId;
});
// 正常会话
await instance1
.get("hello/check", { step: 1, flag: userId })
.then((res) => console.log(res));
// 退出登录
await instance1
.get("/hello/loginOut", { step: 1, flag: userId })
.then((res) => {
console.log(res);
// handler1.logout();
handler1.unloadPage(userId);
return res.data;
});
// 退出登录之后的重新登录尝试
await instance2.post("hello/login", {}, { step: 0 }).then((res) => {
console.log(res);
return res.data.userId;
});
}
try {
test();
} catch (err) {
console.log(err);
}