Skip to content

Commit

Permalink
Add docs about how to get the user profile and emails
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiodxa committed Nov 27, 2024
1 parent b89ba63 commit 9ada746
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,42 @@ authenticator.use(
// later in your code you can use it to get new tokens object
let tokens = await strategy.refreshToken(user.refreshToken);
```

### Get user profile

Once you have the OAuth2 tokens object, you can use the access token to get the [user profile from GitHub's API](https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-the-authenticated-user).

```ts
let response = await fetch("https://api.github.com/user", {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${tokens.accessToken()}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});

let user = await response.json();

// Somehow parse the user object to ensure it has the correct shape
```

### Get user email

Similarly, you can use the access token to get the [user's email from GitHub's API](https://docs.github.com/en/rest/users/emails?apiVersion=2022-11-28#list-email-addresses-for-the-authenticated-user).

> [!IMPORTANT]
> You will need to request the `user:email` scope when authenticating the user.
```ts
let response = await fetch("https://api.github.com/user/emails", {
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${tokens.accessToken()}`,
"X-GitHub-Api-Version": "2022-11-28",
},
});

let emails = await response.json();

// Somehow parse the emails object to ensure it has the correct shape
```
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@
"engines": {
"node": "^18.0.0 || ^20.0.0 || >=20.0.0"
},
"files": [
"build",
"package.json",
"README.md"
],
"funding": [
"https://github.com/sponsors/sergiodxa"
],
"files": ["build", "package.json", "README.md"],
"funding": ["https://github.com/sponsors/sergiodxa"],
"homepage": "https://github.com/sergiodxa/remix-auth-github#readme",
"keywords": [
"remix",
Expand Down

0 comments on commit 9ada746

Please sign in to comment.