-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
discord.js
40 lines (35 loc) · 971 Bytes
/
discord.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict'
const fastify = require('fastify')({ logger: { level: 'trace' } })
const oauthPlugin = require('..')
fastify.register(oauthPlugin, {
name: 'discordOAuth2',
credentials: {
client: {
id: '<CLIENT_ID>',
secret: '<CLIENT_SECRET>'
},
auth: oauthPlugin.DISCORD_CONFIGURATION
},
startRedirectPath: '/login/facebook',
callbackUri: 'http://localhost:3000/login/discord/callback'
})
fastify.get('/login/discord/callback', async function (request, reply) {
try {
const token =
await this.discordOAuth2.getAccessTokenFromAuthorizationCodeFlow(request)
return reply.send(token)
} catch (error) {
return reply.send(error)
}
})
fastify.get('/login/discord', {}, (req, reply) => {
fastify.discordOAuth2.generateAuthorizationUri(
req,
reply,
(err, authorizationEndpoint) => {
if (err) console.error(err)
reply.redirect(authorizationEndpoint)
}
)
})
fastify.listen({ port: 3000 })