Skip to content

Commit

Permalink
Get email with github token (#505)
Browse files Browse the repository at this point in the history
* Get email with github token

* Changeset added
  • Loading branch information
monoald authored May 6, 2024
1 parent 9495f5d commit 42e75f0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-files-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/oauth-providers': minor
---

Requesting github for user email with token
40 changes: 21 additions & 19 deletions packages/oauth-providers/src/providers/github/authFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ export class AuthFlow {
}
}

private async getEmail() {
const emails = (await fetch('https://api.github.com/user/emails', {
headers: {
Authorization: `Bearer ${this.token?.token}`,
'User-Agent': userAgent,
},
}).then((res) => res.json())) as GitHubEmailResponse[] | GitHubErrorResponse

if ('message' in emails) {
throw new HTTPException(400, { message: emails.message })
}

let email = emails.find((emails) => emails.primary === true)?.email
if (email === undefined) {
email = emails.find((emails) => !emails.email.includes('@users.noreply.github.com'))?.email
}

return email as string
}

async getUserData() {
if (!this.token?.token) {
await this.getTokenFromCode()
Expand All @@ -112,25 +132,7 @@ export class AuthFlow {
throw new HTTPException(400, { message: response.message })
}

if (!this.oauthApp) {
const emails = (await fetch('https://api.github.com/user/emails', {
headers: {
Authorization: `Bearer ${this.token?.token}`,
'User-Agent': userAgent,
},
}).then((res) => res.json())) as GitHubEmailResponse[] | GitHubErrorResponse

if ('message' in emails) {
throw new HTTPException(400, { message: emails.message })
}

let email = emails.find((emails) => emails.primary === true)?.email
if (email === undefined) {
email = emails.find((emails) => !emails.email.includes('@users.noreply.github.com'))?.email
}

response.email = email as string
}
response.email = await this.getEmail()

if ('id' in response) {
this.user = response
Expand Down

0 comments on commit 42e75f0

Please sign in to comment.