diff --git a/src/connections/Twitch/index.ts b/src/connections/Twitch/index.ts
index f9412a17..9a6cea35 100644
--- a/src/connections/Twitch/index.ts
+++ b/src/connections/Twitch/index.ts
@@ -55,17 +55,20 @@ export default class TwitchConnection extends RefreshableConnection {
settings: TwitchSettings = new TwitchSettings();
init(): void {
- this.settings = ConnectionLoader.getConnectionConfig(
+ const settings = ConnectionLoader.getConnectionConfig<TwitchSettings>(
this.id,
this.settings,
- ) as TwitchSettings;
+ );
+
+ if (settings.enabled && (!settings.clientId || !settings.clientSecret))
+ throw new Error(`Invalid settings for connection ${this.id}`);
}
getAuthorizationUrl(userId: string): string {
const state = this.createState(userId);
const url = new URL(this.authorizeUrl);
- url.searchParams.append("client_id", this.settings.clientId!);
+ url.searchParams.append("client_id", this.settings.clientId as string);
url.searchParams.append("redirect_uri", this.getRedirectUri());
url.searchParams.append("response_type", "code");
url.searchParams.append("scope", this.scopes.join(" "));
@@ -94,8 +97,8 @@ export default class TwitchConnection extends RefreshableConnection {
new URLSearchParams({
grant_type: "authorization_code",
code: code,
- client_id: this.settings.clientId!,
- client_secret: this.settings.clientSecret!,
+ client_id: this.settings.clientId as string,
+ client_secret: this.settings.clientSecret as string,
redirect_uri: this.getRedirectUri(),
}),
)
@@ -124,8 +127,8 @@ export default class TwitchConnection extends RefreshableConnection {
.body(
new URLSearchParams({
grant_type: "refresh_token",
- client_id: this.settings.clientId!,
- client_secret: this.settings.clientSecret!,
+ client_id: this.settings.clientId as string,
+ client_secret: this.settings.clientSecret as string,
refresh_token: refresh_token,
}),
)
@@ -148,7 +151,7 @@ export default class TwitchConnection extends RefreshableConnection {
return wretch(url.toString())
.headers({
Authorization: `Bearer ${token}`,
- "Client-Id": this.settings.clientId!,
+ "Client-Id": this.settings.clientId as string,
})
.get()
.json<TwitchConnectionUserResponse>()
@@ -161,8 +164,11 @@ export default class TwitchConnection extends RefreshableConnection {
async handleCallback(
params: ConnectionCallbackSchema,
): Promise<ConnectedAccount | null> {
- const userId = this.getUserId(params.state);
- const tokenData = await this.exchangeCode(params.state, params.code!);
+ const { state, code } = params;
+ if (!code) throw new Error("No code provided");
+
+ const userId = this.getUserId(state);
+ const tokenData = await this.exchangeCode(state, code);
const userInfo = await this.getUser(tokenData.access_token);
const exists = await this.hasConnection(userId, userInfo.data[0].id);
|