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