mirror of
https://github.com/Dan6erbond/sk-auth.git
synced 2024-11-20 19:07:20 +01:00
Add Discord provider
This commit is contained in:
parent
ca3cca1d6f
commit
8ff1640751
8996
package-lock.json
generated
Normal file
8996
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
56
src/providers/discord.ts
Normal file
56
src/providers/discord.ts
Normal 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();
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
export { Provider } from "./base";
|
||||
export { DiscordOAuth2Provider } from "./discord";
|
||||
export type { DiscordProfile, DiscordTokens } from "./discord";
|
||||
export { GitHubOAuth2Provider } from "./github";
|
||||
export type { GitHubProfile, GitHubTokens } from "./github";
|
||||
export { GoogleOAuth2Provider } from "./google";
|
||||
@ -14,4 +16,4 @@ export { SpotifyOAuth2Provider } from "./spotify";
|
||||
export type { SpotifyProfile, SpotifyTokens } from "./spotify";
|
||||
export { TwitchOAuth2Provider } from "./twitch";
|
||||
export type { TwitchProfile, TwitchTokens } from "./twitch";
|
||||
export { TwitterAuthProvider } from "./twitter";
|
||||
export { TwitterAuthProvider } from "./twitter";
|
Loading…
Reference in New Issue
Block a user