From c34bf4647ef17507fcb4faed64540f9417a60710 Mon Sep 17 00:00:00 2001 From: Humenius Date: Fri, 10 Jul 2020 13:12:08 +0200 Subject: [PATCH] Stop Bearer null-checks and fetch token on every incoming request --- backend/src/services/sinusbot.service.ts | 43 +++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/backend/src/services/sinusbot.service.ts b/backend/src/services/sinusbot.service.ts index 177b2e8..4e2af7d 100644 --- a/backend/src/services/sinusbot.service.ts +++ b/backend/src/services/sinusbot.service.ts @@ -25,15 +25,23 @@ export class SinusBotService { private bearer: string; public async fetchStats(): Promise { - if (this.bearer == null) { - logger.info(`Hey! I'm trying to get my Bearer token!`); - await this.login() - .then(token => this.bearer = token); - logger.debug( - `Seems like I have my Bearer token! - Looks like it's ${this.bearer == null ? 'undefined' : 'not undefined'}` - ); - } + // Skip check as either way + // - An interval needs to reset this.bearer to null + // - The Sinusbot token is not a JWT => Expiration date is not decodable + // - Estimated expiration time: 1d? + // - I don't know if it makes a difference to check via interval or to just fetch the token + // everytime a request is sent against this API. + // if (this.bearer == null) { + logger.info(`Hey! I'm trying to get my Bearer token!`); + await this.login() + .then(token => this.bearer = token) + .then(() => { + logger.debug( + `Seems like I have my Bearer token! + Looks like it's ${this.bearer == null ? 'undefined' : 'not undefined'}` + ); + }); + // } logger.info(`I try to fetch user data now! The URL is called ${this.tunaKillURL}`); return await fetch(this.tunaKillURL, this.requestConfig(null, this.bearer)) @@ -42,14 +50,7 @@ export class SinusBotService { .then(data => this.consumeTunakillResponse(data)) .catch(error => { logger.error(`I couldn't fetch user data.`, error); - - if (error instanceof RequestError && error.response && error.response.status === 401) { - logger.info(`Trying to refresh token...`); - this.bearer = undefined; - this.login().then(token => this.bearer = token); - } else { - throw this.createHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error.message); - } + throw this.createHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error.message); }); } @@ -84,7 +85,11 @@ export class SinusBotService { .then(data => data.token) .catch(error => { logger.error(`Oh oh! Something went wrong while fetching Bearer token.`, error); - throw this.createHttpException(HttpStatus.UNAUTHORIZED, `Fetching Bearer token for Sinusbot failed.`); + throw this.createHttpException( + HttpStatus.UNAUTHORIZED, + `Fetching Bearer token for Sinusbot failed. + Please refresh page or try again later!` + ); }); } @@ -126,7 +131,7 @@ export class SinusBotService { headers: { 'Accept': '*/*', 'Content-Type': 'application/json', - 'User-Agent': 'HumeniusTSRankingBackend/0.0.1', + 'User-Agent': 'HumeniusTSRankingBackend/0.0.2', 'Authorization': `Bearer ${bearerToken}` } };