summary refs log tree commit diff
path: root/src/api/routes
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-10-28 15:25:58 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-10-28 15:25:58 +1100
commite991e00f325d003d68e8ac710c4ee8dfb4bdca4c (patch)
tree637e6b6fd0628afecdee50650c155789042a929d /src/api/routes
parentfix bad user validation (diff)
downloadserver-e991e00f325d003d68e8ac710c4ee8dfb4bdca4c.tar.xz
Move src-slowcord to own repo https://github.com/MaddyUnderStars/slowcord-services
Diffstat (limited to 'src/api/routes')
-rw-r--r--src/api/routes/oauth2/callback.ts38
-rw-r--r--src/api/routes/policies/instance/stats.ts21
2 files changed, 59 insertions, 0 deletions
diff --git a/src/api/routes/oauth2/callback.ts b/src/api/routes/oauth2/callback.ts
new file mode 100644
index 00000000..3c7fb777
--- /dev/null
+++ b/src/api/routes/oauth2/callback.ts
@@ -0,0 +1,38 @@
+import { Router, Request, Response } from "express";
+import { route, OauthCallbackHandlers } from "@fosscord/api";
+import { FieldErrors, generateToken, User } from "@fosscord/util";
+const router = Router();
+
+router.get("/:type", route({}), async (req: Request, res: Response) => {
+	const { type } = req.params;
+	const handler = OauthCallbackHandlers[type];
+	if (!handler) throw FieldErrors({
+		type: {
+			code: "BASE_TYPE_CHOICES",
+			message: `Value must be one of (${Object.keys(OauthCallbackHandlers).join(", ")}).`,
+		}
+	});
+
+	const { code } = req.query;
+	if (!code || typeof code !== "string") throw FieldErrors({ code: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED"), } });
+	const access = await handler.getAccessToken(code);
+
+	const oauthUser = await handler.getUserDetals(access.access_token);
+
+	let user = await User.findOne({ where: { email: oauthUser.email } });
+	if (!user) {
+		user = await User.register({
+			email: oauthUser.email,
+			username: oauthUser.username,
+			req
+		});
+
+		// TODO: upload pfp, banner?
+	}
+
+	const token = await generateToken(user.id);
+
+	return { token };
+});
+
+export default router;
\ No newline at end of file
diff --git a/src/api/routes/policies/instance/stats.ts b/src/api/routes/policies/instance/stats.ts
new file mode 100644
index 00000000..fb8c386a
--- /dev/null
+++ b/src/api/routes/policies/instance/stats.ts
@@ -0,0 +1,21 @@
+import { Router, Request, Response } from "express";
+import { route } from "@fosscord/api";
+import { Attachment, Config, Guild, Message, RateLimit, Session, User } from "@fosscord/util";
+const router = Router();
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	res.json({
+		all_time: {
+			users: await User.count(),
+			guilds: await Guild.count(),
+			messages: await Message.count(),
+			attachments: await Attachment.count(),
+		},
+		now: {
+			sessions: await Session.count(),
+			rate_limits: await RateLimit.count(),
+		}
+	});
+});
+
+export default router;