Add Discord provider

This commit is contained in:
L-Mario564 2022-07-25 14:29:21 -06:00
parent ca3cca1d6f
commit 8ff1640751
4 changed files with 10882 additions and 1891 deletions

8996
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

56
src/providers/discord.ts Normal file
View File

@ -0,0 +1,56 @@
import { OAuth2Provider, OAuth2ProviderConfig } from "./oauth2";
export interface DiscordProfile {
id: string;
username: string;
discriminator: string;
avatar?: string;
bot: boolean;
system: boolean;
mfa_enabled: boolean;
banner?: string;
accent_color?: number;
locale: string;
flags: number;
premium_type: number;
public_flags: number;
}
export interface DiscordTokens {
access_token: string;
token_type: string;
expires_in: number;
refresh_token: string;
scope: string;
}
type DiscordOAuth2ProviderConfig = OAuth2ProviderConfig<DiscordProfile, DiscordTokens>;
const defaultConfig: Partial<DiscordOAuth2ProviderConfig> = {
id: "discord",
scope: "identify",
accessTokenUrl: "https://discord.com/api/oauth2/token",
authorizationUrl: "https://discord.com/api/oauth2/authorize",
profileUrl: "https://discord.com/api/users/@me",
contentType: "application/json"
};
export class DiscordOAuth2Provider extends OAuth2Provider<
DiscordProfile,
DiscordTokens,
DiscordOAuth2ProviderConfig
> {
constructor(config: DiscordOAuth2ProviderConfig) {
super({
...defaultConfig,
...config,
});
}
async getUserProfile(tokens: DiscordTokens): Promise<DiscordProfile> {
const res = await fetch(this.config.profileUrl!, {
headers: { Authorization: `Bearer ${tokens.access_token}` },
});
return await res.json();
}
}

View File

@ -1,4 +1,6 @@
export { Provider } from "./base"; export { Provider } from "./base";
export { DiscordOAuth2Provider } from "./discord";
export type { DiscordProfile, DiscordTokens } from "./discord";
export { GitHubOAuth2Provider } from "./github"; export { GitHubOAuth2Provider } from "./github";
export type { GitHubProfile, GitHubTokens } from "./github"; export type { GitHubProfile, GitHubTokens } from "./github";
export { GoogleOAuth2Provider } from "./google"; export { GoogleOAuth2Provider } from "./google";
@ -14,4 +16,4 @@ export { SpotifyOAuth2Provider } from "./spotify";
export type { SpotifyProfile, SpotifyTokens } from "./spotify"; export type { SpotifyProfile, SpotifyTokens } from "./spotify";
export { TwitchOAuth2Provider } from "./twitch"; export { TwitchOAuth2Provider } from "./twitch";
export type { TwitchProfile, TwitchTokens } from "./twitch"; export type { TwitchProfile, TwitchTokens } from "./twitch";
export { TwitterAuthProvider } from "./twitter"; export { TwitterAuthProvider } from "./twitter";

3717
yarn.lock

File diff suppressed because it is too large Load Diff