token和logout原来的处理有点小问题

This commit is contained in:
Xu Chang 2024-01-26 16:18:43 +08:00
parent b3adeafa8a
commit 489e1f9992
13 changed files with 133 additions and 92 deletions

View File

@ -28,7 +28,9 @@ export type GeneralAspectDict<ED extends EntityDict, Cxt extends BackendRuntimeC
env: WebEnv;
wechatLoginId?: string;
}, context: Cxt) => Promise<string>;
logout: ({}: {}, context: Cxt) => Promise<void>;
logout: (params: {
tokenValue: string;
}, context: Cxt) => Promise<void>;
loginWechatMp: ({ code, env, }: {
code: string;
env: WechatMpEnv;

View File

@ -57,7 +57,9 @@ export declare function getWechatMpUserPhoneNumber<ED extends EntityDict, Cxt ex
code: string;
env: WechatMpEnv;
}, context: Cxt): Promise<string>;
export declare function logout<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>({}: {}, context: Cxt): Promise<void>;
export declare function logout<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>(params: {
tokenValue: string;
}, context: Cxt): Promise<void>;
/**
* parasite上的token
* @param params

View File

@ -134,7 +134,7 @@ function autoMergeUser(context) {
* @param env
* @param context
* @param user
* @return tokenId
* @return tokenValue
*/
async function setUpTokenAndUser(env, context, entity, // 支持更多的登录渠道使用此函数创建token
entityId, // 如果是现有对象传id如果没有对象传createData
@ -409,11 +409,11 @@ async function setupMobile(mobile, env, context) {
});
}
}
async function loadTokenInfo(tokenId, context) {
async function loadTokenInfo(tokenValue, context) {
return await context.select('token', {
data: cloneDeep(tokenProjection),
filter: {
id: tokenId,
value: tokenValue,
},
}, {});
}
@ -519,10 +519,10 @@ export async function loginByMobile(params, context) {
throw new OakUserException('账号不存在');
}
}
const tokenId = await loginLogic();
await loadTokenInfo(tokenId, context);
const tokenValue = await loginLogic();
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
async function setUserInfoFromWechat(user, userInfo, context) {
const application = context.getApplication();
@ -883,9 +883,9 @@ async function loginFromWechatEnv(code, env, context, wechatLoginId) {
action: 'create',
data: userData,
}, {});
const tokenId = await createWechatUserAndReturnTokenId(userData);
const tokenValue = await createWechatUserAndReturnTokenId(userData);
await updateWechatLogin({ userId, successed: true });
return tokenId;
return tokenValue;
}
}
}
@ -983,10 +983,10 @@ export async function loginWechat({ code, env, wechatLoginId, }, context) {
*/
export async function loginWechatMp({ code, env, }, context) {
const closeRootMode = context.openRootMode();
const tokenId = await loginFromWechatEnv(code, env, context);
await loadTokenInfo(tokenId, context);
const tokenValue = await loginFromWechatEnv(code, env, context);
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
/**
* 同步从wx.getUserProfile拿到的用户信息
@ -1207,9 +1207,9 @@ export async function getWechatMpUserPhoneNumber({ code, env }, context) {
closeRootMode();
return reuslt;
}
export async function logout({}, context) {
const tokenId = context.getTokenValue();
if (tokenId) {
export async function logout(params, context) {
const { tokenValue } = params;
if (tokenValue) {
const closeRootMode = context.openRootMode();
try {
await context.operate('token', {
@ -1217,7 +1217,8 @@ export async function logout({}, context) {
action: 'disable',
data: {},
filter: {
id: tokenId,
value: tokenValue,
ableState: 'enabled',
},
}, { dontCollect: true });
}
@ -1278,24 +1279,25 @@ export async function wakeupParasite(params, context) {
},
}, { dontCollect: true });
}
const tokenId = await generateNewIdAsync();
const tokenValue = await generateNewIdAsync();
await context.operate('token', {
id: await generateNewIdAsync(),
action: 'create',
data: {
id: tokenId,
id: await generateNewIdAsync(),
entity: 'parasite',
entityId: id,
userId: parasite.userId,
playerId: parasite.userId,
disablesAt: Date.now() + parasite.tokenLifeLength,
env,
tokenValue,
applicationId: context.getApplicationId(),
},
}, { dontCollect: true });
await loadTokenInfo(tokenId, context);
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
/**
* todo 检查登录环境一致性同一个token不能跨越不同设备
@ -1329,7 +1331,7 @@ export async function refreshToken(params, context) {
fn();
return '';
}
else if (now - token.refreshedAt < 600 * 1000) {
else if (now - token.refreshedAt > 600 * 1000) {
const newValue = await generateNewIdAsync();
await context.operate('token', {
id: await generateNewIdAsync(),

View File

@ -12,7 +12,7 @@ declare const _default: <ED2 extends EntityDict & BaseEntityDict, T2 extends key
type?: ButtonProps['type'] | AmButtonProps['type'];
executeText?: string | undefined;
buttonProps?: (ButtonProps & {
color?: "default" | "success" | "warning" | "primary" | "danger" | undefined;
color?: "default" | "success" | "primary" | "warning" | "danger" | undefined;
fill?: "none" | "solid" | "outline" | undefined;
size?: "small" | "large" | "middle" | "mini" | undefined;
block?: boolean | undefined;

View File

@ -20,13 +20,20 @@ export class Token extends Feature {
}
} */
if (tokenValue) {
this.tokenValue = tokenValue;
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('refreshToken', {
tokenValue,
env,
}, undefined, true, true);
this.tokenValue = result;
try {
const { result } = await this.cache.exec('refreshToken', {
tokenValue,
env,
}, undefined, true, true);
this.tokenValue = result;
}
catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
}
else {
this.tokenValue = undefined;
@ -101,7 +108,9 @@ export class Token extends Feature {
this.publish();
}
async logout() {
await this.cache.exec('logout', {});
await this.cache.exec('logout', {
tokenValue: this.tokenValue,
}, undefined, undefined, true);
this.removeToken();
}
removeToken(disablePublish) {

View File

@ -14,7 +14,7 @@ export declare function createToDo<ED extends EntityDict & BaseEntityDict, T ext
redirectTo: EntityDict['toDo']['OpSchema']['redirectTo'];
entity: any;
entityId: string;
}, userIds?: string[]): Promise<1 | 0>;
}, userIds?: string[]): Promise<0 | 1>;
/**
* todo例程entity对象上进行action操作时filtertodo完成
* entity的action的后trigger中调用

View File

@ -28,7 +28,9 @@ export type GeneralAspectDict<ED extends EntityDict, Cxt extends BackendRuntimeC
env: WebEnv;
wechatLoginId?: string;
}, context: Cxt) => Promise<string>;
logout: ({}: {}, context: Cxt) => Promise<void>;
logout: (params: {
tokenValue: string;
}, context: Cxt) => Promise<void>;
loginWechatMp: ({ code, env, }: {
code: string;
env: WechatMpEnv;

View File

@ -57,7 +57,9 @@ export declare function getWechatMpUserPhoneNumber<ED extends EntityDict, Cxt ex
code: string;
env: WechatMpEnv;
}, context: Cxt): Promise<string>;
export declare function logout<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>({}: {}, context: Cxt): Promise<void>;
export declare function logout<ED extends EntityDict, Cxt extends BackendRuntimeContext<ED>>(params: {
tokenValue: string;
}, context: Cxt): Promise<void>;
/**
* parasite上的token
* @param params

View File

@ -137,7 +137,7 @@ function autoMergeUser(context) {
* @param env
* @param context
* @param user
* @return tokenId
* @return tokenValue
*/
async function setUpTokenAndUser(env, context, entity, // 支持更多的登录渠道使用此函数创建token
entityId, // 如果是现有对象传id如果没有对象传createData
@ -412,11 +412,11 @@ async function setupMobile(mobile, env, context) {
});
}
}
async function loadTokenInfo(tokenId, context) {
async function loadTokenInfo(tokenValue, context) {
return await context.select('token', {
data: (0, lodash_1.cloneDeep)(Projection_1.tokenProjection),
filter: {
id: tokenId,
value: tokenValue,
},
}, {});
}
@ -522,10 +522,10 @@ async function loginByMobile(params, context) {
throw new types_1.OakUserException('账号不存在');
}
}
const tokenId = await loginLogic();
await loadTokenInfo(tokenId, context);
const tokenValue = await loginLogic();
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
exports.loginByMobile = loginByMobile;
async function setUserInfoFromWechat(user, userInfo, context) {
@ -889,9 +889,9 @@ async function loginFromWechatEnv(code, env, context, wechatLoginId) {
action: 'create',
data: userData,
}, {});
const tokenId = await createWechatUserAndReturnTokenId(userData);
const tokenValue = await createWechatUserAndReturnTokenId(userData);
await updateWechatLogin({ userId, successed: true });
return tokenId;
return tokenValue;
}
}
}
@ -990,10 +990,10 @@ exports.loginWechat = loginWechat;
*/
async function loginWechatMp({ code, env, }, context) {
const closeRootMode = context.openRootMode();
const tokenId = await loginFromWechatEnv(code, env, context);
await loadTokenInfo(tokenId, context);
const tokenValue = await loginFromWechatEnv(code, env, context);
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
exports.loginWechatMp = loginWechatMp;
/**
@ -1219,9 +1219,9 @@ async function getWechatMpUserPhoneNumber({ code, env }, context) {
return reuslt;
}
exports.getWechatMpUserPhoneNumber = getWechatMpUserPhoneNumber;
async function logout({}, context) {
const tokenId = context.getTokenValue();
if (tokenId) {
async function logout(params, context) {
const { tokenValue } = params;
if (tokenValue) {
const closeRootMode = context.openRootMode();
try {
await context.operate('token', {
@ -1229,7 +1229,8 @@ async function logout({}, context) {
action: 'disable',
data: {},
filter: {
id: tokenId,
value: tokenValue,
ableState: 'enabled',
},
}, { dontCollect: true });
}
@ -1291,24 +1292,25 @@ async function wakeupParasite(params, context) {
},
}, { dontCollect: true });
}
const tokenId = await (0, uuid_1.generateNewIdAsync)();
const tokenValue = await (0, uuid_1.generateNewIdAsync)();
await context.operate('token', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'create',
data: {
id: tokenId,
id: await (0, uuid_1.generateNewIdAsync)(),
entity: 'parasite',
entityId: id,
userId: parasite.userId,
playerId: parasite.userId,
disablesAt: Date.now() + parasite.tokenLifeLength,
env,
tokenValue,
applicationId: context.getApplicationId(),
},
}, { dontCollect: true });
await loadTokenInfo(tokenId, context);
await loadTokenInfo(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
exports.wakeupParasite = wakeupParasite;
/**

View File

@ -23,13 +23,20 @@ class Token extends oak_frontend_base_1.Feature {
}
} */
if (tokenValue) {
this.tokenValue = tokenValue;
const env = await this.environment.getEnv();
const { result } = await this.cache.exec('refreshToken', {
tokenValue,
env,
}, undefined, true, true);
this.tokenValue = result;
try {
const { result } = await this.cache.exec('refreshToken', {
tokenValue,
env,
}, undefined, true, true);
this.tokenValue = result;
}
catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
}
else {
this.tokenValue = undefined;
@ -104,7 +111,9 @@ class Token extends oak_frontend_base_1.Feature {
this.publish();
}
async logout() {
await this.cache.exec('logout', {});
await this.cache.exec('logout', {
tokenValue: this.tokenValue,
}, undefined, undefined, true);
this.removeToken();
}
removeToken(disablePublish) {

View File

@ -43,7 +43,7 @@ export type GeneralAspectDict<
},
context: Cxt
) => Promise<string>;
logout: ({ }, context: Cxt) => Promise<void>;
logout: (params: { tokenValue: string }, context: Cxt) => Promise<void>;
loginWechatMp: (
{
code,

View File

@ -205,7 +205,7 @@ function autoMergeUser<ED extends EntityDict, Cxt extends BackendRuntimeContext<
* @param env
* @param context
* @param user
* @return tokenId
* @return tokenValue
*/
async function setUpTokenAndUser<
ED extends EntityDict,
@ -577,13 +577,13 @@ async function setupMobile<
async function loadTokenInfo<
ED extends EntityDict,
Cxt extends BackendRuntimeContext<ED>
>(tokenId: string, context: Cxt) {
>(tokenValue: string, context: Cxt) {
return await context.select(
'token',
{
data: cloneDeep(tokenProjection),
filter: {
id: tokenId,
value: tokenValue,
},
},
{}
@ -721,11 +721,11 @@ export async function loginByMobile<
throw new OakUserException('账号不存在');
}
}
const tokenId = await loginLogic();
await loadTokenInfo<ED, Cxt>(tokenId, context);
const tokenValue = await loginLogic();
await loadTokenInfo<ED, Cxt>(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
async function setUserInfoFromWechat<
@ -1251,11 +1251,11 @@ async function loginFromWechatEnv<
},
{}
);
const tokenId = await createWechatUserAndReturnTokenId(
const tokenValue = await createWechatUserAndReturnTokenId(
userData as EntityDict['user']['Schema']
);
await updateWechatLogin({ userId, successed: true });
return tokenId;
return tokenValue;
}
}
} else {
@ -1415,10 +1415,10 @@ export async function loginWechatMp<
context: Cxt
): Promise<string> {
const closeRootMode = context.openRootMode();
const tokenId = await loginFromWechatEnv<ED, Cxt>(code, env, context);
await loadTokenInfo<ED, Cxt>(tokenId, context);
const tokenValue = await loginFromWechatEnv<ED, Cxt>(code, env, context);
await loadTokenInfo<ED, Cxt>(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
/**
@ -1733,9 +1733,9 @@ export async function getWechatMpUserPhoneNumber<
export async function logout<
ED extends EntityDict,
Cxt extends BackendRuntimeContext<ED>
>({ }, context: Cxt) {
const tokenId = context.getTokenValue();
if (tokenId) {
>(params: { tokenValue: string }, context: Cxt) {
const { tokenValue } = params;
if (tokenValue) {
const closeRootMode = context.openRootMode();
try {
@ -1746,7 +1746,8 @@ export async function logout<
action: 'disable',
data: {},
filter: {
id: tokenId,
value: tokenValue,
ableState: 'enabled',
},
},
{ dontCollect: true }
@ -1831,29 +1832,30 @@ export async function wakeupParasite<
);
}
const tokenId = await generateNewIdAsync();
const tokenValue = await generateNewIdAsync();
await context.operate(
'token',
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: tokenId,
id: await generateNewIdAsync(),
entity: 'parasite',
entityId: id,
userId: parasite.userId,
playerId: parasite.userId,
disablesAt: Date.now() + parasite.tokenLifeLength!,
env,
tokenValue,
applicationId: context.getApplicationId(),
},
},
{ dontCollect: true }
);
await loadTokenInfo<ED, Cxt>(tokenId, context);
await loadTokenInfo<ED, Cxt>(tokenValue, context);
closeRootMode();
return tokenId;
return tokenValue;
}
/**

View File

@ -41,19 +41,26 @@ export class Token<
} */
if (tokenValue) {
this.tokenValue = tokenValue;
const env = await this.environment.getEnv();
const { result } = await this.cache.exec(
'refreshToken',
{
tokenValue,
env,
},
undefined,
true,
true
);
this.tokenValue = result;
try {
const { result } = await this.cache.exec(
'refreshToken',
{
tokenValue,
env,
},
undefined,
true,
true
);
this.tokenValue = result;
}
catch (err) {
// refresh出了任何错都无视直接放弃此token
console.warn(err);
this.tokenValue = undefined;
this.removeToken(true);
}
} else {
this.tokenValue = undefined;
}
@ -156,7 +163,9 @@ export class Token<
}
async logout() {
await this.cache.exec('logout', {});
await this.cache.exec('logout', {
tokenValue: this.tokenValue!,
}, undefined, undefined, true);
this.removeToken();
}