summary refs log tree commit diff
path: root/src/api/routes/auth/generate-registration-tokens.ts
blob: e328fe5e2ec3d4fef935bde36fbded9664dec63c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { route, random } from "@fosscord/api";
import { Config, ValidRegistrationToken } from "@fosscord/util";
import { Request, Response, Router } from "express";

const router: Router = Router();
export default router;

router.get("/", route({ right: "OPERATOR" }), async (req: Request, res: Response) => {
	const count = req.query.count ? parseInt(req.query.count as string) : 1;
	const length = req.query.length ? parseInt(req.query.length as string) : 255;

	let tokens: ValidRegistrationToken[] = [];

	for (let i = 0; i < count; i++) {
		const token = ValidRegistrationToken.create({
			token: random(length),
			expires_at: Date.now() + Config.get().security.defaultRegistrationTokenExpiration
		});
		tokens.push(token);
	}

	// Why are these options used, exactly?
	await ValidRegistrationToken.save(tokens, { chunk: 1000, reload: false, transaction: false });

	if (req.query.plain) return res.send(tokens.map(x => x.token).join("\n"));

	return res.json({ tokens: tokens.map(x => x.token) });
});