mirror of
https://github.com/Myxelium/RandomMemerBot.git
synced 2026-04-13 10:30:36 +00:00
Add authentication endpoints & refactor
This commit is contained in:
100
client/controllers/authentication.ts
Normal file
100
client/controllers/authentication.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import express from 'express';
|
||||
import ip from 'ip';
|
||||
import DiscordOauth2 from 'discord-oauth2';
|
||||
import { ssl } from '../server';
|
||||
import * as dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const router = express.Router();
|
||||
const oauth = new DiscordOauth2();
|
||||
|
||||
var clientId = process.env.OAUTH_CLIENT_ID;
|
||||
var clientSecret = process.env.OAUTH_CLIENT_SECRET;
|
||||
var customRedirectUrl = process.env.OAUTH_REDIRECT_URI;
|
||||
var scopes = "identify email guilds";
|
||||
|
||||
router.get('/login', (req, res) => {
|
||||
if(!variablesIsSet(res))
|
||||
return;
|
||||
|
||||
var redirect = `${ssl}://${ip.address()}/oauth`
|
||||
|
||||
if(customRedirectUrl && customRedirectUrl != "default")
|
||||
redirect = `${customRedirectUrl}/oauth`
|
||||
|
||||
res.redirect(`https://discordapp.com/api/oauth2/authorize?client_id=${clientId}&scope=${scopes.replace(' ', '+')}&response_type=code&redirect_uri=${redirect}`);
|
||||
});
|
||||
|
||||
router.get('/oauth', async (_req: any, res: express.Response) => {
|
||||
if(!variablesIsSet(res))
|
||||
return;
|
||||
|
||||
var options = {
|
||||
url: 'https://discord.com/api/oauth2/token',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: new URLSearchParams({
|
||||
'client_id': clientId!,
|
||||
'client_secret': clientSecret!,
|
||||
'grant_type': 'client_credentials',
|
||||
'code': _req.query.code,
|
||||
'redirect_uri': `${ssl}://${ip.address()}`,
|
||||
'scope': scopes
|
||||
})
|
||||
}
|
||||
|
||||
var response = await fetch('https://discord.com/api/oauth2/token', options)
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
});
|
||||
|
||||
res.send(response);
|
||||
});
|
||||
|
||||
router.get('/userinfo', async (req, res) => {
|
||||
const token = req.headers.authorization;
|
||||
|
||||
if(token == null || token == undefined)
|
||||
return res.sendStatus(401);
|
||||
|
||||
var cleanToken = token.replace('Bearer ', '');
|
||||
|
||||
const user = await oauth.getUser(cleanToken);
|
||||
|
||||
res.send(user);
|
||||
});
|
||||
|
||||
router.get('/guilds', async (req, res) => {
|
||||
const token = req.headers.authorization;
|
||||
|
||||
if(token == null || token == undefined)
|
||||
return res.sendStatus(401);
|
||||
|
||||
var cleanToken = token.replace('Bearer ', '');
|
||||
|
||||
const guilds = await oauth.getUserGuilds(cleanToken);
|
||||
|
||||
res.send(guilds);
|
||||
});
|
||||
|
||||
function variablesIsSet(response: any): boolean {
|
||||
let errorText = "Invalid configuration. Please check your environment variables.";
|
||||
|
||||
if(clientId == undefined || clientSecret == undefined) {
|
||||
response.send(errorText);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if(clientId.length < 5 && clientSecret.length < 5) {
|
||||
response.send(errorText);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export default router;
|
||||
44
client/controllers/index.ts
Normal file
44
client/controllers/index.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import { Handlers } from "../handlers/index";
|
||||
import { nextPlayBackTime } from '../../bot';
|
||||
import { loadAvoidList } from '../../helpers/load-avoid-list';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* Returns the next playback time.
|
||||
* @returns string - The next playback time.
|
||||
*/
|
||||
router.get('/nextplaybacktime', (_req, res) => {
|
||||
res.send(nextPlayBackTime);
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns the index.html file.
|
||||
* @returns index.html - The index.html file.
|
||||
*/
|
||||
router.get('/', (_req, res) => {
|
||||
res.sendFile(path.join(__dirname, '../web/index.html'));
|
||||
});
|
||||
|
||||
router.get('/avoidlist', (_req, res) => {
|
||||
res.send(loadAvoidList());
|
||||
});
|
||||
|
||||
router.post('/avoidlist', (req, res) => {
|
||||
Handlers.AddUserToAvoidList(res, req);
|
||||
});
|
||||
|
||||
router.delete('/avoidlist/:user', (req, res) => {
|
||||
Handlers.DeleteUserFromAvoidList(res, req);
|
||||
});
|
||||
|
||||
router.get('/join', (_req, res) => {
|
||||
Handlers.JoinChannel(res);
|
||||
});
|
||||
|
||||
router.use(express.static(path.join(__dirname, "../web")));
|
||||
|
||||
|
||||
export default router;
|
||||
22
client/controllers/sounds.ts
Normal file
22
client/controllers/sounds.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import { Handlers } from "../handlers/index";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.delete('/sounds/:filename', (_req, res) => {
|
||||
Handlers.DeleteSoundFile(res, _req);
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a file from the sounds folder by filename
|
||||
* @param filename - The name of the file to return.
|
||||
* @returns mp3 - The requested file.
|
||||
*/
|
||||
router.use('/sounds', express.static(path.join(__dirname, '../../sounds')));
|
||||
|
||||
router.get('/sounds', (_req, res: express.Response) => {
|
||||
return Handlers.GetSoundFiles(res);
|
||||
});
|
||||
|
||||
export default router;
|
||||
44
client/controllers/upload.ts
Normal file
44
client/controllers/upload.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import express from 'express';
|
||||
import multer, { diskStorage } from 'multer';
|
||||
import path from 'path';
|
||||
import { Handlers } from "../handlers/index";
|
||||
import { generateFileName } from '../../helpers/generate-file-name';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const storage = diskStorage({
|
||||
destination: 'sounds/',
|
||||
filename: function (_req, file, cb) {
|
||||
cb(null, generateFileName(file.originalname));
|
||||
}
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage: storage,
|
||||
limits: { fileSize: 1 * 1024 * 1024 },
|
||||
fileFilter: function (_req, file, cb) {
|
||||
if (path.extname(file.originalname) !== '.mp3') {
|
||||
return cb(new Error('Only .mp3 files are allowed'));
|
||||
}
|
||||
|
||||
cb(null, true);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Uploads a file to the sounds folder.
|
||||
* @Body myFile - The file to upload.
|
||||
*/
|
||||
router.post('/upload', upload.single('myFile'), async (req, res) => {
|
||||
res.send('File uploaded successfully.');
|
||||
});
|
||||
|
||||
router.post('/youtube', async (req, res) => {
|
||||
await Handlers.UploadYouTubeFile(res, req);
|
||||
});
|
||||
|
||||
router.post('/upload-youtube', async (req, res) => {
|
||||
await Handlers.UploadYouTubeFile(res, req);
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user