Skip to content

Commit 2f150c7

Browse files
authored
chore: add Github example (#175)
* feat: add Github example * refactor: use `getAccessTokenFromRefreshToken` instead of API request #174 (comment)
1 parent 66169fc commit 2f150c7

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

examples/github.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const sget = require('simple-get')
5+
6+
// const oauthPlugin = require('fastify-oauth2')
7+
const oauthPlugin = require('..')
8+
9+
fastify.register(oauthPlugin, {
10+
name: 'githubOAuth2',
11+
scope: [],
12+
credentials: {
13+
client: {
14+
id: '<CLIENT_ID>',
15+
secret: '<CLIENT_SECRET>'
16+
},
17+
auth: oauthPlugin.GITHUB_CONFIGURATION
18+
},
19+
startRedirectPath: '/login/github',
20+
callbackUri: 'http://localhost:3000/login/github/callback'
21+
})
22+
23+
const memStore = new Map()
24+
25+
async function saveAccessToken (token) {
26+
memStore.set(token.refresh_token, token)
27+
}
28+
29+
async function retrieveAccessToken (token) {
30+
// remove Bearer if needed
31+
if (token.startsWith('Bearer ')) {
32+
token = token.substring(6)
33+
}
34+
// any database or in-memory operation here
35+
// we use in-memory variable here
36+
if (memStore.has(token)) {
37+
memStore.get(token)
38+
}
39+
throw new Error('invalid refresh token')
40+
}
41+
42+
fastify.get('/login/github/callback', async function (request, reply) {
43+
const token = await this.githubOAuth2.getAccessTokenFromAuthorizationCodeFlow(request)
44+
45+
console.log(token.access_token)
46+
47+
// you should store the `token` for further usage
48+
await saveAccessToken(token)
49+
50+
reply.send({ access_token: token.access_token })
51+
})
52+
53+
fastify.get('/login/github/refreshAccessToken', async function (request, reply) {
54+
// we assume the token is passed by authorization header
55+
const refreshToken = await retrieveAccessToken(request.headers.authorization)
56+
const newToken = await this.githubOAuth2.getAccessTokenFromRefreshToken(refreshToken, {})
57+
58+
// we save the token again
59+
await saveAccessToken(newToken)
60+
61+
reply.send({ access_token: newToken.access_token })
62+
})
63+
64+
// Check access token: https://docs.github.com/en/rest/apps/oauth-applications#check-a-token
65+
fastify.get('/login/github/verifyAccessToken', function (request, reply) {
66+
const { accessToken } = request.query
67+
68+
sget.concat(
69+
{
70+
url: 'https://api.github.com/applications/<CLIENT_ID>/token',
71+
method: 'POST',
72+
headers: {
73+
Authorization:
74+
'Basic ' +
75+
Buffer.from('<CLIENT_ID>' + ':' + '<CLIENT_SECRET').toString(
76+
'base64'
77+
)
78+
},
79+
body: JSON.stringify({ access_token: accessToken }),
80+
json: true
81+
},
82+
function (err, res, data) {
83+
if (err) {
84+
reply.send(err)
85+
return
86+
}
87+
reply.send(data)
88+
}
89+
)
90+
})
91+
92+
fastify.listen({ port: 3000 })

0 commit comments

Comments
 (0)