-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const express = require('express'); | ||
const fetch = require('node-fetch'); | ||
const btoa = require('btoa'); | ||
const { catchAsync } = require('../utils'); | ||
|
||
const router = express.Router(); | ||
|
||
const CLIENT_ID = ' your app id '; | ||
const CLIENT_SECRET = ' your app secret '; | ||
const redirect = encodeURIComponent(' callback url (normally "http://localhost:50451/api/discord/callback" for example) '); | ||
|
||
router.get('/login', (req, res) => { | ||
res.redirect(`https://discordapp.com/oauth2/authorize?client_id=${CLIENT_ID}&scope=guilds%20identify&response_type=code&redirect_uri=${redirect}`); | ||
}); | ||
|
||
router.get('/callback', catchAsync(async (req, res) => { | ||
if (!req.query.code) throw new Error('NoCodeProvided'); | ||
const code = req.query.code; | ||
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`); | ||
const response = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`, | ||
{ | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Basic ${creds}`, | ||
}, | ||
}); | ||
const json = await response.json(); | ||
res.redirect(`/s/?token=${json.access_token}`); | ||
})); | ||
|
||
module.exports = router; |