-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): fix link function for GitHub
- Loading branch information
Showing
6 changed files
with
147 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import fetch from "node-fetch"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { ConfigService } from "../config/config.service"; | ||
|
||
@Injectable() | ||
export class OAuthRequestService { | ||
constructor(private config: ConfigService) { | ||
} | ||
|
||
async getGitHubToken(code: string): Promise<GitHubToken> { | ||
const qs = new URLSearchParams(); | ||
qs.append("client_id", this.config.get("oauth.github-clientId")); | ||
qs.append("client_secret", this.config.get("oauth.github-clientSecret")); | ||
qs.append("code", code); | ||
|
||
const res = await fetch("https://github.com/login/oauth/access_token?" + qs.toString(), { | ||
method: "post", | ||
headers: { | ||
"Accept": "application/json", | ||
} | ||
}); | ||
return await res.json() as GitHubToken; | ||
} | ||
|
||
async getGitHubUser(token: GitHubToken): Promise<GitHubUser> { | ||
const res = await fetch("https://api.github.com/user", { | ||
headers: { | ||
"Accept": "application/vnd.github+json", | ||
"Authorization": `${token.token_type} ${token.access_token}`, | ||
} | ||
}); | ||
return await res.json() as GitHubUser; | ||
} | ||
|
||
async getGitHubEmail(token: GitHubToken): Promise<string | undefined> { | ||
const res = await fetch("https://api.github.com/user/public_emails", { | ||
headers: { | ||
"Accept": "application/vnd.github+json", | ||
"Authorization": `${token.token_type} ${token.access_token}`, | ||
} | ||
}); | ||
const emails = await res.json() as GitHubEmail[]; | ||
return emails.find(e => e.primary && e.verified)?.email; | ||
} | ||
} | ||
|
||
|
||
interface GitHubToken { | ||
access_token: string; | ||
token_type: string; | ||
scope: string; | ||
} | ||
|
||
interface GitHubUser { | ||
login: string; | ||
id: number; | ||
name?: string; | ||
email?: string; // this filed seems only return null | ||
} | ||
|
||
interface GitHubEmail { | ||
email: string; | ||
primary: boolean, | ||
verified: boolean, | ||
visibility: string | null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters