Stop Bearer null-checks and fetch token on every incoming request
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-07-10 13:12:08 +02:00
parent c61c9657e2
commit c34bf4647e

View File

@@ -25,15 +25,23 @@ export class SinusBotService {
private bearer: string;
public async fetchStats(): Promise<TableEntry[]> {
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}`
}
};