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: oat
GPG key ID: DD83A9617A252385
6 changed files with 69 additions and 1 deletions

2
.env.example Normal file
View file

@ -0,0 +1,2 @@
DISCORD_OAUTH_CLIENTID=""
DISCORD_OAUTH_CLIENTSECRET=""

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ node_modules
built built
config/config.json config/config.json
*.log *.log
.env

18
package-lock.json generated
View file

@ -422,6 +422,14 @@
"resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz", "resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz",
"integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==" "integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw=="
}, },
"axios": {
"version": "0.20.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz",
"integrity": "sha512-ANA4rr2BDcmmAQLOKft2fufrtuvlqR+cXNNinUmvfeSNCOF98PZL+7M/v1zIdGo7OLjEA9J2gXJL+j4zGsl0bA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@ -699,6 +707,11 @@
"esutils": "^2.0.2" "esutils": "^2.0.2"
} }
}, },
"dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
},
"ee-first": { "ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@ -1051,6 +1064,11 @@
"resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz", "resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz",
"integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw==" "integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw=="
}, },
"follow-redirects": {
"version": "1.13.0",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.0.tgz",
"integrity": "sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA=="
},
"forwarded": { "forwarded": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",

View file

@ -13,6 +13,8 @@
"dependencies": { "dependencies": {
"@types/express": "github:types/express", "@types/express": "github:types/express",
"@types/mongoose": "^5.7.36", "@types/mongoose": "^5.7.36",
"axios": "^0.20.0",
"dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-fileupload": "^1.2.0", "express-fileupload": "^1.2.0",
"mongoose": "^5.10.2", "mongoose": "^5.10.2",

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