Compare commits
2 commits
108eec139d
...
758e2ef7e2
Author | SHA1 | Date | |
---|---|---|---|
|
758e2ef7e2 | ||
|
6ff85af711 |
7 changed files with 77 additions and 2 deletions
2
.env.example
Normal file
2
.env.example
Normal file
|
@ -0,0 +1,2 @@
|
|||
DISCORD_OAUTH_CLIENTID=""
|
||||
DISCORD_OAUTH_CLIENTSECRET=""
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,4 +2,5 @@
|
|||
node_modules
|
||||
built
|
||||
config/config.json
|
||||
*.log
|
||||
*.log
|
||||
.env
|
18
package-lock.json
generated
18
package-lock.json
generated
|
@ -422,6 +422,14 @@
|
|||
"resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz",
|
||||
"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": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
|
||||
|
@ -699,6 +707,11 @@
|
|||
"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": {
|
||||
"version": "1.1.1",
|
||||
"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",
|
||||
"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": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
"dependencies": {
|
||||
"@types/express": "github:types/express",
|
||||
"@types/mongoose": "^5.7.36",
|
||||
"axios": "^0.20.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"express-fileupload": "^1.2.0",
|
||||
"mongoose": "^5.10.2",
|
||||
|
|
40
src/auth.ts
Normal file
40
src/auth.ts
Normal 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');
|
||||
}
|
||||
});
|
||||
}
|
|
@ -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({});
|
||||
|
|
|
@ -29,4 +29,11 @@ const FileSchema = new Schema({
|
|||
charts: [Chart]
|
||||
});
|
||||
|
||||
export const File = mongoose.model('File', FileSchema);
|
||||
export const File = mongoose.model('File', FileSchema);
|
||||
|
||||
const UserSchema = new Schema({ // this is pretty much just a discord user lol
|
||||
id: String, // cus longass number
|
||||
approved: Boolean
|
||||
});
|
||||
|
||||
export const User = mongoose.model('User', UserSchema);
|
Loading…
Reference in a new issue