fix: 更新用户信息命令以使用新的获取令牌逻辑,并优化本地用户检查功能
This commit is contained in:
@@ -257,48 +257,52 @@ export class AssistantApp extends Manager {
|
||||
* 检查本地用户登录状态,如果未登录且存在 CNB_TOKEN,则尝试使用 CNB_TOKEN 登录并更新用户信息
|
||||
*/
|
||||
async checkLocalUser() {
|
||||
const config = this.config.getConfig();
|
||||
const auth = config?.auth;
|
||||
let checkCNB = false;
|
||||
if (!auth?.username) {
|
||||
checkCNB = true;
|
||||
} else {
|
||||
let temp = await assistantQuery.queryLogin.getToken()
|
||||
if (temp) {
|
||||
const isExpired = await assistantQuery.queryLogin.checkTokenValid()
|
||||
console.log('Token 是否过期', isExpired);
|
||||
return checkLocalUser({ assistantApp: this });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const checkLocalUser = async (opts: { assistantApp: AssistantApp }) => {
|
||||
const { assistantApp } = opts;
|
||||
const config = assistantApp.config.getConfig();
|
||||
const auth = config?.auth;
|
||||
let checkCNB = false;
|
||||
if (!auth?.username) {
|
||||
checkCNB = true;
|
||||
// 没有登录过,自动检测ci进行登录
|
||||
// 检测条件1:环境变量中存在 CNB_TOKEN
|
||||
} else {
|
||||
let temp = await assistantQuery.getToken()
|
||||
logger.info('[assistant] 当前登录用户', auth.username, 'token有效性检查结果', !!temp);
|
||||
}
|
||||
const cnbToken = useKey('CNB_TOKEN');
|
||||
if (!checkCNB && cnbToken) {
|
||||
const res = await assistantQuery.query.post({
|
||||
path: 'user',
|
||||
key: 'cnb-login',
|
||||
payload: {
|
||||
data: {
|
||||
cnbToken: cnbToken,
|
||||
}
|
||||
}
|
||||
logger.info('[assistant] 当前登录用户', auth.username, 'token有效性检查结果', !!temp);
|
||||
}
|
||||
const cnbToken = useKey('CNB_TOKEN');
|
||||
if (!checkCNB && cnbToken) {
|
||||
const res = await assistantQuery.query.post({
|
||||
path: 'user',
|
||||
key: 'cnb-login',
|
||||
payload: {
|
||||
data: {
|
||||
cnbToken: cnbToken,
|
||||
}
|
||||
}
|
||||
});
|
||||
if (res.code === 200) {
|
||||
logger.info('CNB登录成功,用户信息已更新');
|
||||
const resUser = await assistantQuery.queryLogin.beforeSetLoginUser(res.data)
|
||||
if (resUser.code === 200) {
|
||||
const userInfo = resUser.data;
|
||||
auth.username = userInfo.username;
|
||||
auth.share = 'protected'
|
||||
const app = config?.app || {};
|
||||
if (!app?.id) {
|
||||
app.id = 'dev-cnb'
|
||||
}
|
||||
this.config.setConfig({
|
||||
auth,
|
||||
app
|
||||
});
|
||||
} else {
|
||||
console.error('CNB登录失败,无法获取用户信息', resUser);
|
||||
});
|
||||
if (res.code === 200) {
|
||||
logger.info('CNB登录成功,用户信息已更新');
|
||||
const resUser = await assistantQuery.queryLogin.beforeSetLoginUser(res.data)
|
||||
if (resUser.code === 200) {
|
||||
const userInfo = resUser.data;
|
||||
auth.username = userInfo.username;
|
||||
auth.share = 'protected'
|
||||
const app = config?.app || {};
|
||||
if (!app?.id) {
|
||||
app.id = 'dev-cnb'
|
||||
}
|
||||
assistantApp.config.setConfig({
|
||||
auth,
|
||||
app
|
||||
});
|
||||
} else {
|
||||
console.error('CNB登录失败,无法获取用户信息', resUser);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,12 @@ export class AssistantQuery {
|
||||
get(body: any, options?: DataOpts) {
|
||||
return this.query.get(body, options);
|
||||
}
|
||||
getToken() {
|
||||
return this.queryLogin.getToken();
|
||||
async getToken() {
|
||||
const token = await this.queryLogin.getToken();
|
||||
if (!token) return '';
|
||||
const isExpired = await this.queryLogin.checkTokenValid()
|
||||
console.log('Token 是否过期', isExpired, token);
|
||||
console.log('info', this.queryLogin.cacheStore.cacheData)
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user