Fix all of my non-functional auth refresh code
This commit is contained in:
parent
dbd8a486ae
commit
6a5503c7a6
1 changed files with 15 additions and 10 deletions
|
|
@ -27,7 +27,8 @@ export class TwitchAuth {
|
||||||
twitchAuths[this._channel] = this;
|
twitchAuths[this._channel] = this;
|
||||||
};
|
};
|
||||||
if (token_data?.bid == null) {
|
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 {
|
} else {
|
||||||
this._bid = token_data.bid;
|
this._bid = token_data.bid;
|
||||||
};
|
};
|
||||||
|
|
@ -39,6 +40,7 @@ export class TwitchAuth {
|
||||||
log.debug(`Requesting user info`)
|
log.debug(`Requesting user info`)
|
||||||
let r = await this.request<any>("GET", "/users");
|
let r = await this.request<any>("GET", "/users");
|
||||||
this._channel = r.data.data[0].login;
|
this._channel = r.data.data[0].login;
|
||||||
|
this._bid = r.data.data[0].id;
|
||||||
if (this._channel) {
|
if (this._channel) {
|
||||||
twitchAuths[this._channel] = this;
|
twitchAuths[this._channel] = this;
|
||||||
};
|
};
|
||||||
|
|
@ -81,14 +83,16 @@ export class TwitchAuth {
|
||||||
try {
|
try {
|
||||||
let r = await this.request<TokenData>(
|
let r = await this.request<TokenData>(
|
||||||
"POST",
|
"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._refresh_token = r.refresh_token;
|
||||||
this._token = r.access_token,
|
this._token = r.access_token,
|
||||||
this.expires_in = r.expires_in;
|
this.expires_in = r.expires_in;
|
||||||
this.scope = r.scope;
|
this.scope = r.scope;
|
||||||
this.token_type = r.token_type;
|
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}`)
|
log.error(`Could not refresh the token for ${this._channel}`)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -101,6 +105,7 @@ export class TwitchAuth {
|
||||||
expires_in: this.expires_in,
|
expires_in: this.expires_in,
|
||||||
scope: this.scope,
|
scope: this.scope,
|
||||||
channel: this.channel,
|
channel: this.channel,
|
||||||
|
bid: this.bid,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -112,7 +117,7 @@ export class TwitchAuth {
|
||||||
* @param conf The Axios RequestConfig, without method, url, or baseURL
|
* @param conf The Axios RequestConfig, without method, url, or baseURL
|
||||||
* @returns The data returned from the Twitch API
|
* @returns The data returned from the Twitch API
|
||||||
*/
|
*/
|
||||||
public async request<T>(method: Method, url: string, conf:any={}, attempt=0): Promise<T> {
|
public async request<T>(method: Method, url: string, conf:any={}, refreshed=false): Promise<T> {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let headers = {
|
let headers = {
|
||||||
|
|
@ -141,17 +146,17 @@ export class TwitchAuth {
|
||||||
|
|
||||||
// global error handling,
|
// global error handling,
|
||||||
switch (err.response.status) {
|
switch (err.response.status) {
|
||||||
|
case 401:
|
||||||
case 403:
|
case 403:
|
||||||
this.refresh();
|
await this.refresh();
|
||||||
if (attempt < 3) {
|
if (!refreshed) {
|
||||||
this.request(method, url, conf, ++attempt);
|
log.debug(`Attempting refresh'd request`)
|
||||||
|
return this.request(method, url, conf, true);
|
||||||
};
|
};
|
||||||
break;
|
|
||||||
default:
|
default:
|
||||||
log.error(err);
|
log.error(err.response.data);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
log.error(err);
|
|
||||||
throw err;
|
throw err;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue