discord auth bay bee

this just, , tells you what your username is right now but will definetly be used later on to login
This commit is contained in:
oat 2020-09-03 10:28:22 +03:00
parent 6ff85af711
commit 758e2ef7e2
Signed by untrusted user who does not match committer: oat
GPG key ID: DD83A9617A252385
6 changed files with 69 additions and 1 deletions

40
src/auth.ts Normal file
View file

@ -0,0 +1,40 @@
const API_ENDPOINT = 'https://discord.com/api/v6';
const axios = require('axios').default;
export function run(app) {
app.get('/discordauth', async (req, res) => {
const code = req.query.code;
if (code) {
try {
const data = new URLSearchParams({
client_id: process.env.DISCORD_OAUTH_CLIENTID,
client_secret: process.env.DISCORD_OAUTH_CLIENTSECRET,
grant_type: 'authorization_code',
code: code,
redirect_uri: 'http://localhost:8080/discordauth',
scope: 'identify'
});
const postRes = await axios.post(`${API_ENDPOINT}/oauth2/token`, data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
const userInfo = await axios.get(`${API_ENDPOINT}/users/@me`, {
headers: {
authorization: `${postRes.data.token_type} ${postRes.data.access_token}`
}
});
res.send(`hi ${userInfo.data.username}#${userInfo.data.discriminator}`);
} catch(err) {
res.send(`whoooops<br>${err}`);
}
} else {
res.send('https://discord.com/api/oauth2/authorize?client_id=750952563079250012&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fdiscordauth&response_type=code&scope=identify');
}
});
}

View file

@ -8,6 +8,10 @@ import * as format from './lib/format';
import { File } from './schema';
import * as upload from './upload';
import * as auth from './auth';
// .env stuff
require('dotenv').config();
const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'}));
@ -55,6 +59,7 @@ db.then(() => {
app.set('logger', logger);
upload.run(app);
auth.run(app);
app.get('/list', async (req, res) => { // only for testing
const docs = await File.find({});