feat: 更新版本至 0.0.60,优化登录缓存逻辑,添加 token 过期检查示例
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/api",
|
"name": "@kevisual/api",
|
||||||
"version": "0.0.59",
|
"version": "0.0.60",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "mod.ts",
|
"main": "mod.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -90,6 +90,15 @@ export type LoginCacheStoreOpts<T extends Cache = Cache> = {
|
|||||||
name: string;
|
name: string;
|
||||||
cache: T;
|
cache: T;
|
||||||
};
|
};
|
||||||
|
const defaultCacheData: CacheLogin = {
|
||||||
|
loginUsers: [],
|
||||||
|
user: undefined,
|
||||||
|
id: undefined,
|
||||||
|
accessToken: undefined,
|
||||||
|
refreshToken: undefined,
|
||||||
|
accessTokenExpiresIn: undefined,
|
||||||
|
createdAt: undefined,
|
||||||
|
}
|
||||||
export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
||||||
cache: T;
|
cache: T;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -100,12 +109,16 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|||||||
}
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.cache = opts.cache;
|
this.cache = opts.cache;
|
||||||
this.cacheData = {
|
this.cacheData = { ...defaultCacheData };
|
||||||
loginUsers: [],
|
this.name = opts.name;
|
||||||
user: undefined,
|
}
|
||||||
id: undefined,
|
/**
|
||||||
accessToken: undefined,
|
* 设置缓存
|
||||||
refreshToken: undefined,
|
* @param key
|
||||||
|
* @param value
|
||||||
|
* @returns
|
||||||
|
accessTokenExpiresIn: undefined,
|
||||||
|
createdAt: undefined,
|
||||||
};
|
};
|
||||||
this.name = opts.name;
|
this.name = opts.name;
|
||||||
}
|
}
|
||||||
@@ -125,13 +138,7 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|||||||
*/
|
*/
|
||||||
async delValue() {
|
async delValue() {
|
||||||
await this.cache.del();
|
await this.cache.del();
|
||||||
this.cacheData = {
|
this.cacheData = { ...defaultCacheData };
|
||||||
loginUsers: [],
|
|
||||||
user: undefined,
|
|
||||||
id: undefined,
|
|
||||||
accessToken: undefined,
|
|
||||||
refreshToken: undefined,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
getValue(): Promise<CacheLogin> {
|
getValue(): Promise<CacheLogin> {
|
||||||
return this.cache.get(this.name);
|
return this.cache.get(this.name);
|
||||||
@@ -140,15 +147,7 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|||||||
* 初始化,设置默认值
|
* 初始化,设置默认值
|
||||||
*/
|
*/
|
||||||
async init() {
|
async init() {
|
||||||
const defaultData: CacheLogin = {
|
const defaultData: CacheLogin = { ...this.cacheData };
|
||||||
loginUsers: [],
|
|
||||||
user: undefined,
|
|
||||||
id: undefined,
|
|
||||||
accessToken: undefined,
|
|
||||||
refreshToken: undefined,
|
|
||||||
accessTokenExpiresIn: undefined,
|
|
||||||
createdAt: undefined,
|
|
||||||
};
|
|
||||||
if (this.cache.init) {
|
if (this.cache.init) {
|
||||||
try {
|
try {
|
||||||
const cacheData = await this.cache.init();
|
const cacheData = await this.cache.init();
|
||||||
@@ -165,18 +164,18 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|||||||
* 设置当前用户
|
* 设置当前用户
|
||||||
* @param user
|
* @param user
|
||||||
*/
|
*/
|
||||||
async setLoginUser(user: CacheLoginUser) {
|
async setLoginUser(loginUser: CacheLoginUser) {
|
||||||
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
const has = this.cacheData.loginUsers.find((u) => u.id === loginUser.id);
|
||||||
if (has) {
|
if (has) {
|
||||||
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
|
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== loginUser.id);
|
||||||
}
|
}
|
||||||
this.cacheData.loginUsers.push(user);
|
this.cacheData.loginUsers.push(loginUser);
|
||||||
this.cacheData.user = user.user;
|
this.cacheData.user = loginUser.user;
|
||||||
this.cacheData.id = user.id;
|
this.cacheData.id = loginUser.id;
|
||||||
this.cacheData.accessToken = user.accessToken;
|
this.cacheData.accessToken = loginUser.accessToken;
|
||||||
this.cacheData.refreshToken = user.refreshToken;
|
this.cacheData.refreshToken = loginUser.refreshToken;
|
||||||
this.cacheData.accessTokenExpiresIn = user.accessTokenExpiresIn;
|
this.cacheData.accessTokenExpiresIn = loginUser.accessTokenExpiresIn;
|
||||||
this.cacheData.createdAt = user.createdAt;
|
this.cacheData.createdAt = loginUser.createdAt;
|
||||||
await this.setValue(this.cacheData);
|
await this.setValue(this.cacheData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,22 +213,22 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
|
|||||||
if (has) {
|
if (has) {
|
||||||
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
|
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
|
||||||
}
|
}
|
||||||
this.cacheData.user = undefined;
|
const hasOther = this.cacheData.loginUsers.length > 0;
|
||||||
this.cacheData.id = undefined;
|
const current = this.cacheData.loginUsers[this.cacheData.loginUsers.length - 1];
|
||||||
this.cacheData.accessToken = undefined;
|
if (hasOther && current) {
|
||||||
this.cacheData.refreshToken = undefined;
|
this.cacheData.user = current.user;
|
||||||
this.cacheData.accessTokenExpiresIn = undefined;
|
this.cacheData.id = current.id;
|
||||||
this.cacheData.createdAt = undefined;
|
this.cacheData.accessToken = current.accessToken;
|
||||||
|
this.cacheData.refreshToken = current.refreshToken;
|
||||||
|
this.cacheData.accessTokenExpiresIn = current.accessTokenExpiresIn;
|
||||||
|
this.cacheData.createdAt = current.createdAt;
|
||||||
|
} else {
|
||||||
|
this.cacheData = { ...defaultCacheData };
|
||||||
|
}
|
||||||
await this.setValue(this.cacheData);
|
await this.setValue(this.cacheData);
|
||||||
}
|
}
|
||||||
async clearAll() {
|
async clearAll() {
|
||||||
this.cacheData.loginUsers = [];
|
this.cacheData = { ...defaultCacheData };
|
||||||
this.cacheData.user = undefined;
|
|
||||||
this.cacheData.id = undefined;
|
|
||||||
this.cacheData.accessToken = undefined;
|
|
||||||
this.cacheData.refreshToken = undefined;
|
|
||||||
this.cacheData.accessTokenExpiresIn = undefined;
|
|
||||||
this.cacheData.createdAt = undefined;
|
|
||||||
await this.setValue(this.cacheData);
|
await this.setValue(this.cacheData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,6 +360,7 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
|
|||||||
}
|
}
|
||||||
const isExpired = await this.cacheStore.getIsExpired();
|
const isExpired = await this.cacheStore.getIsExpired();
|
||||||
if (isExpired) {
|
if (isExpired) {
|
||||||
|
console.log('token过期,正在刷新token', this.cacheStore.cacheData);
|
||||||
const res = await this.refreshLoginUser()
|
const res = await this.refreshLoginUser()
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
// 刷新成功,返回新的token
|
// 刷新成功,返回新的token
|
||||||
|
|||||||
10
query/query-login/test/expire.ts
Normal file
10
query/query-login/test/expire.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
const cacheData = {
|
||||||
|
accessTokenExpiresIn: 604800,
|
||||||
|
createdAt: 1771926793545
|
||||||
|
};
|
||||||
|
|
||||||
|
const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
|
||||||
|
console.log('expiresIn', expiresIn);
|
||||||
|
const now = Date.now();
|
||||||
|
console.log('now', now);
|
||||||
|
console.log('isExpired', now >= expiresIn);
|
||||||
Reference in New Issue
Block a user