diff options
author | Puyodead1 <puyodead@proton.me> | 2023-08-06 23:15:35 -0400 |
---|---|---|
committer | Puyodead1 <puyodead@proton.me> | 2023-08-06 23:15:35 -0400 |
commit | fe4acbfee58e840412ed5d496fb565843d4a8cdd (patch) | |
tree | 3397988bd5ff2c812a0fbf688c1d375ad02df7d8 /src/connections/GitHub | |
parent | Fix connection class imports (diff) | |
download | server-fe4acbfee58e840412ed5d496fb565843d4a8cdd.tar.xz |
Fix linter warnings in connections
Diffstat (limited to 'src/connections/GitHub')
-rw-r--r-- | src/connections/GitHub/index.ts | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/connections/GitHub/index.ts b/src/connections/GitHub/index.ts index 101b205c..25e5f89f 100644 --- a/src/connections/GitHub/index.ts +++ b/src/connections/GitHub/index.ts @@ -19,10 +19,10 @@ import { ConnectedAccount, ConnectedAccountCommonOAuthTokenResponse, + Connection, ConnectionCallbackSchema, ConnectionLoader, DiscordApiErrors, - Connection, } from "@spacebar/util"; import wretch from "wretch"; import { GitHubSettings } from "./GitHubSettings"; @@ -42,17 +42,20 @@ export default class GitHubConnection extends Connection { settings: GitHubSettings = new GitHubSettings(); init(): void { - this.settings = ConnectionLoader.getConnectionConfig( + const settings = ConnectionLoader.getConnectionConfig<GitHubSettings>( this.id, this.settings, - ) as GitHubSettings; + ); + + 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("scope", this.scopes.join(" ")); url.searchParams.append("state", state); @@ -61,8 +64,11 @@ export default class GitHubConnection extends Connection { getTokenUrl(code: string): string { const url = new URL(this.tokenUrl); - url.searchParams.append("client_id", this.settings.clientId!); - url.searchParams.append("client_secret", this.settings.clientSecret!); + url.searchParams.append("client_id", this.settings.clientId as string); + url.searchParams.append( + "client_secret", + this.settings.clientSecret as string, + ); url.searchParams.append("code", code); return url.toString(); } @@ -105,8 +111,11 @@ export default class GitHubConnection extends Connection { 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.id.toString()); |