Skip to content

Commit

Permalink
switch to djs client.api, clean up form validation and error handling…
Browse files Browse the repository at this point in the history
…, and other stuff

major backend update

closes #100
#94
  • Loading branch information
advaith1 committed Sep 7, 2020
1 parent bf82bdf commit 53a8ad3
Show file tree
Hide file tree
Showing 13 changed files with 942 additions and 1,458 deletions.
7 changes: 6 additions & 1 deletion @types/custom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ declare module "express-serve-static-core" {
interface Response {
session: any;
user: any;
__: any;
/** get i18n string
*
* https://github.com/discordextremelist/i18n/tree/master/website
* https://translate.discordextremelist.xyz
*/
__(key: string, args?: any): string;
}
}
56 changes: 28 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"monaco-editor": "^0.20.0",
"mongodb": "~3.5.10",
"morgan": "~1.10.0",
"node-fetch": "^2.6.0",
"node-fetch": "^2.6.1",
"passport": "^0.4.1",
"passport-discord": "^0.1.4",
"request": "^2.88.2",
Expand All @@ -48,13 +48,13 @@
"@types/cookie-session": "^2.0.41",
"@types/datadog-metrics": "^0.6.0",
"@types/ejs": "^3.0.4",
"@types/express": "^4.17.7",
"@types/express": "^4.17.8",
"@types/http-errors": "^1.8.0",
"@types/i18n": "^0.8.7",
"@types/ioredis": "^4.17.3",
"@types/mongodb": "^3.5.26",
"@types/ioredis": "^4.17.4",
"@types/mongodb": "^3.5.27",
"@types/morgan": "^1.9.1",
"@types/node": "^14.6.2",
"@types/node": "^14.6.4",
"@types/passport": "^1.0.4",
"@types/passport-discord": "^0.1.3",
"@types/sanitize-html": "^1.23.3",
Expand Down
54 changes: 17 additions & 37 deletions src/Routes/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { Request, Response } from "express";

import bodyParser from "body-parser";
import passport from "passport";
import * as https from "https";
import { Strategy } from "passport-discord";
import * as discord from "../Util/Services/discord";
import { DiscordAPIError } from "discord.js";

import * as settings from "../../settings.json";
import * as tokenManager from "../Util/Services/adminTokenManager";
Expand Down Expand Up @@ -58,7 +59,7 @@ router.use(
})
);

router.get("/login/joinGuild", (req: Request, res: Response, next) => {
router.get("/login/joinGuild", (req: Request, res: Response) => {
req.session.joinGuild = true;
res.redirect("/auth/login/continue");
});
Expand Down Expand Up @@ -256,47 +257,26 @@ router.get(
if (req.session.joinGuild && req.session.joinGuild === true) {
req.session.joinGuild = false;

const data = JSON.stringify({
access_token: req.user.accessToken
});

const options = {
hostname: "discord.com",
port: 443,
path: `/api/v6/guilds/${settings.guild.main}/members/${req.user.id}`,
method: "PUT",
headers: {
"Content-Type": "application/json",
"Content-Length": data.length,
Authorization: "Bot " + settings.secrets.discord.token
}
};

const msReq = https.request(options, (response) => {
if (response.statusCode === 403 && !req.user.impersonator) {
return res.status(403).render("status", {
title: res.__("common.error"),
status: 403,
subtitle: res.__("common.error.notMember"),
req,
type: "Error"
});
} else next();
});

msReq.on("error", (e) => {
console.error(e);
});

msReq.write(data);
msReq.end();
discord.bot.api.guilds(settings.guild.main).members(req.user.id).put({ data: { access_token: req.user.accessToken } })
.catch((error: DiscordAPIError) => {
console.error(error)
if (error.httpStatus === 403 && !req.user.impersonator) {
return res.status(403).render("status", {
title: res.__("common.error"),
status: 403,
subtitle: res.__("common.error.notMember"),
req,
type: "Error"
});
} else next();
});
}

res.redirect(req.session.redirectTo || "/");
}
);

router.get("/logout", async (req: Request, res: Response, next) => {
router.get("/logout", async (req: Request, res: Response) => {
if (!req.user.impersonator) {
req.session.logoutJust = true;
if (req.user.db.rank.admin) await tokenManager.tokenReset(req.user.id);
Expand Down
Loading

0 comments on commit 53a8ad3

Please sign in to comment.