mirror of
https://github.com/Dan6erbond/sk-auth.git
synced 2024-11-20 19:07:20 +01:00
✨ add github provider (#50)
This commit is contained in:
parent
731eabd893
commit
7c3d02ea0a
50
src/providers/github.ts
Normal file
50
src/providers/github.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { OAuth2Provider, OAuth2ProviderConfig } from "./oauth2";
|
||||||
|
|
||||||
|
export interface GitHubProfile {
|
||||||
|
id: number;
|
||||||
|
login: string;
|
||||||
|
avatar_url: string;
|
||||||
|
url: string;
|
||||||
|
name: string;
|
||||||
|
// complete with more info
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GitHubTokens {
|
||||||
|
access_token: string;
|
||||||
|
token_type: string;
|
||||||
|
expires_in: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type GitHubOAuth2ProviderConfig = OAuth2ProviderConfig<GitHubProfile, GitHubTokens>;
|
||||||
|
|
||||||
|
const defaultConfig: Partial<GitHubOAuth2ProviderConfig> = {
|
||||||
|
id: "github",
|
||||||
|
scope: "user",
|
||||||
|
accessTokenUrl: "https://github.com/login/oauth/access_token",
|
||||||
|
authorizationUrl: "https://github.com/login/oauth/authorize",
|
||||||
|
profileUrl: "https://api.github.com/user",
|
||||||
|
headers: {
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export class GitHubOAuth2Provider extends OAuth2Provider<
|
||||||
|
GitHubProfile,
|
||||||
|
GitHubTokens,
|
||||||
|
GitHubOAuth2ProviderConfig
|
||||||
|
> {
|
||||||
|
constructor(config: GitHubOAuth2ProviderConfig) {
|
||||||
|
super({
|
||||||
|
...defaultConfig,
|
||||||
|
...config,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getUserProfile(tokens: GitHubTokens): Promise<GitHubProfile> {
|
||||||
|
const tokenType = "token"; // 🤷♂️ token type returned is "bearer" but GitHub uses "token" keyword
|
||||||
|
const res = await fetch(this.config.profileUrl!, {
|
||||||
|
headers: { Authorization: `${tokenType} ${tokens.access_token}` },
|
||||||
|
});
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
}
|
@ -11,3 +11,5 @@ export type { ProfileCallback } from "./oauth2.base";
|
|||||||
export { OAuth2Provider } from "./oauth2";
|
export { OAuth2Provider } from "./oauth2";
|
||||||
export { RedditOAuth2Provider } from "./reddit";
|
export { RedditOAuth2Provider } from "./reddit";
|
||||||
export type { RedditProfile, RedditTokens } from "./reddit";
|
export type { RedditProfile, RedditTokens } from "./reddit";
|
||||||
|
export { GitHubOAuth2Provider } from "./github";
|
||||||
|
export type { GitHubProfile, GitHubTokens } from "./github";
|
Loading…
Reference in New Issue
Block a user