Skip to content

Instantly share code, notes, and snippets.

@tcaer
Last active April 14, 2020 04:48
Show Gist options
  • Save tcaer/4b7207e6c731e4df96fc1f9aa05457f8 to your computer and use it in GitHub Desktop.
Save tcaer/4b7207e6c731e4df96fc1f9aa05457f8 to your computer and use it in GitHub Desktop.
The callback route
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT = process.env.DISCORD_CALLBACK_URI;
app.get('/discord/callback', async function(req, res) {
// We must check to ensure that a code was provided for us to use
if (!req.query.code) res.send('No code provided!');
const code = req.query.code;
// We will now create the credentials we will use to get our access and refresh tokens
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);
try {
const resp = await fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${REDIRECT}`, {
method: 'POST',
headers: {
Authorization: `Basic ${creds}`
}
});
const token = await resp.json();
res.json(token);
} catch (err) {
console.error(err);
res.send('Some error occured getting your tokens!');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment