Stop Bearer null-checks and fetch token on every incoming request
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -25,15 +25,23 @@ export class SinusBotService {
|
|||||||
private bearer: string;
|
private bearer: string;
|
||||||
|
|
||||||
public async fetchStats(): Promise<TableEntry[]> {
|
public async fetchStats(): Promise<TableEntry[]> {
|
||||||
if (this.bearer == null) {
|
// Skip check as either way
|
||||||
logger.info(`Hey! I'm trying to get my Bearer token!`);
|
// - An interval needs to reset this.bearer to null
|
||||||
await this.login()
|
// - The Sinusbot token is not a JWT => Expiration date is not decodable
|
||||||
.then(token => this.bearer = token);
|
// - Estimated expiration time: 1d?
|
||||||
logger.debug(
|
// - I don't know if it makes a difference to check via interval or to just fetch the token
|
||||||
`Seems like I have my Bearer token!
|
// everytime a request is sent against this API.
|
||||||
Looks like it's ${this.bearer == null ? 'undefined' : 'not undefined'}`
|
// 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}`);
|
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))
|
return await fetch(this.tunaKillURL, this.requestConfig(null, this.bearer))
|
||||||
@@ -42,14 +50,7 @@ export class SinusBotService {
|
|||||||
.then(data => this.consumeTunakillResponse(data))
|
.then(data => this.consumeTunakillResponse(data))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error(`I couldn't fetch user data.`, error);
|
logger.error(`I couldn't fetch user data.`, error);
|
||||||
|
throw this.createHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error.message);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +85,11 @@ export class SinusBotService {
|
|||||||
.then(data => data.token)
|
.then(data => data.token)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error(`Oh oh! Something went wrong while fetching Bearer token.`, 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: {
|
headers: {
|
||||||
'Accept': '*/*',
|
'Accept': '*/*',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'User-Agent': 'HumeniusTSRankingBackend/0.0.1',
|
'User-Agent': 'HumeniusTSRankingBackend/0.0.2',
|
||||||
'Authorization': `Bearer ${bearerToken}`
|
'Authorization': `Bearer ${bearerToken}`
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user