diff --git a/server/src/utils/TwitchAuth.ts b/server/src/utils/TwitchAuth.ts index 6718cd1..c4ce6c1 100644 --- a/server/src/utils/TwitchAuth.ts +++ b/server/src/utils/TwitchAuth.ts @@ -27,7 +27,8 @@ export class TwitchAuth { twitchAuths[this._channel] = this; }; if (token_data?.bid == null) { - log.silly("Channel auth doesn't contain broadcaster ID"); + log.warn("Channel auth doesn't contain broadcaster ID"); + this.getChannel(); } else { this._bid = token_data.bid; }; @@ -39,6 +40,7 @@ export class TwitchAuth { log.debug(`Requesting user info`) let r = await this.request("GET", "/users"); this._channel = r.data.data[0].login; + this._bid = r.data.data[0].id; if (this._channel) { twitchAuths[this._channel] = this; }; @@ -81,14 +83,16 @@ export class TwitchAuth { try { let r = await this.request( "POST", - config.twitch.auth.base_url + "/token?" + encodeURIComponent(qs.toString()), + config.twitch.auth.base_url + "/token", + { data: qs } ); this._refresh_token = r.refresh_token; this._token = r.access_token, this.expires_in = r.expires_in; this.scope = r.scope; this.token_type = r.token_type; - } catch (err) { + } catch (err: any) { + log.debug(err.response.data) log.error(`Could not refresh the token for ${this._channel}`) } }; @@ -101,6 +105,7 @@ export class TwitchAuth { expires_in: this.expires_in, scope: this.scope, channel: this.channel, + bid: this.bid, }; }; @@ -112,7 +117,7 @@ export class TwitchAuth { * @param conf The Axios RequestConfig, without method, url, or baseURL * @returns The data returned from the Twitch API */ - public async request(method: Method, url: string, conf:any={}, attempt=0): Promise { + public async request(method: Method, url: string, conf:any={}, refreshed=false): Promise { try { let headers = { @@ -141,17 +146,17 @@ export class TwitchAuth { // global error handling, switch (err.response.status) { + case 401: case 403: - this.refresh(); - if (attempt < 3) { - this.request(method, url, conf, ++attempt); + await this.refresh(); + if (!refreshed) { + log.debug(`Attempting refresh'd request`) + return this.request(method, url, conf, true); }; - break; default: - log.error(err); + log.error(err.response.data); }; }; - log.error(err); throw err; }; };