You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB
JavaScript

import { PrismaClient } from "@prisma/client";
import { v4 as uuidv4 } from 'uuid';
const prisma = new PrismaClient();
export default async(req, res) => {
const { subdomain, pointsto, redirect } = req.body;
if(subdomain == "" || pointsto == "") {
res.json({ ok: false, error: "Missing fields." });
return;
}
if(subdomain.length < 3) {
res.json({ ok: false, error: "Subdomain must be at least 3 characters long." });
return;
}
if(pointsto.length < 52) {
res.json({ ok: false, error: "PointsTo doesn't look like a valid SNApp address: it must have at least 52 characters." });
return;
}
if(subdomain.endsWith(".loki")){
res.json({ ok: false, error: "Subdomain doesn't have to include \".loki\"." });
return;
}
if(pointsto.endsWith(".loki")){
res.json({ ok: false, error: "PointsTo doesn't have to include \".loki\"." });
return;
}
var uuid = uuidv4();
var renew = Math.floor(Date.now() / 1000);
try {
await prisma.records.create({
data: {
subdomain,
pointsto,
uuid,
renew,
redirect: redirect ? 1 : 0
},
});
} catch (error) {
res.json({ ok: false, error: "This subdomain already exists, or a subdomain already points to this SNApp's address, or internal error." });
console.log(error);
return;
}
res.status(200).json({ ok: true, uuid: uuid })
}