Skip to content

Commit

Permalink
Create discord.js
Browse files Browse the repository at this point in the history
  • Loading branch information
0zul authored Feb 11, 2019
1 parent c4fa600 commit 171ec28
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions api/discord.js
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;

0 comments on commit 171ec28

Please sign in to comment.