diff --git a/back/services/open.ts b/back/services/open.ts index fb31e82d..00e0a70e 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -145,7 +145,7 @@ export default class OpenService { } } - public async findSystemToken(): Promise<{ + public async generateSystemToken(): Promise<{ value: string; expiration: number; }> { @@ -158,22 +158,13 @@ export default class OpenService { scopes: ['crons', 'system'], } as App); } - const nowTime = Math.round(Date.now() / 1000); - let token; - if ( - !systemApp.tokens || - !systemApp.tokens.length || - nowTime > [...systemApp.tokens].pop()!.expiration - ) { - const authToken = await this.authToken({ - client_id: systemApp.client_id, - client_secret: systemApp.client_secret, - }); - token = authToken.data; - token.value = token.token; - } else { - token = [...systemApp.tokens].pop(); - } - return token; + const { data } = await this.authToken({ + client_id: systemApp.client_id, + client_secret: systemApp.client_secret, + }); + return { + ...data, + value: data.token, + }; } } diff --git a/back/token.ts b/back/token.ts index 85c1ab89..d313a5ba 100755 --- a/back/token.ts +++ b/back/token.ts @@ -10,41 +10,19 @@ const tokenFile = path.join(config.configPath, 'token.json'); async function getToken() { try { - const data = await readFile(); - const nowTime = Math.round(Date.now() / 1000); - if (data.value && data.expiration > nowTime) { - console.log(data.value); - } else { - Container.set('logger', LoggerInstance); - const openService = Container.get(OpenService); - const appToken = await openService.findSystemToken(); - console.log(appToken.value); - await writeFile({ - value: appToken.value, - expiration: appToken.expiration, - }); - } + Container.set('logger', LoggerInstance); + const openService = Container.get(OpenService); + const appToken = await openService.generateSystemToken(); + console.log(appToken.value); + await writeFile({ + value: appToken.value, + expiration: appToken.expiration, + }); } catch (error) { console.log(error); } } -async function readFile() { - return new Promise((resolve, reject) => { - fs.readFile( - path.join(config.configPath, 'token.json'), - { encoding: 'utf8' }, - (err, data) => { - if (err) { - resolve({}); - } else { - resolve(JSON.parse(data)); - } - }, - ); - }); -} - async function writeFile(data: any) { return new Promise((resolve, reject) => { fs.writeFile(tokenFile, JSON.stringify(data), { encoding: 'utf8' }, () => {