Files
NetDomainManager/routes/domains.js
T

101 lines
3.1 KiB
JavaScript
Raw Normal View History

2026-05-14 11:15:30 -04:00
const express = require('express');
const router = express.Router();
const domainValidator = require('../validators/domain');
const authMw = require('../session');
const database = require('../database.js');
const dbConnection = database.db;
const Sequelize = require('sequelize');
const pageTitle = 'Domain Manager | Register New Domain';
2026-05-14 12:44:30 -04:00
const blacklistedDomains = [
"dns.internal"
];
2026-05-14 11:15:30 -04:00
const supportedTLDs = [
2026-05-14 12:44:30 -04:00
"internal",
2026-05-14 11:15:30 -04:00
"tomato",
"secret",
"money",
"lol",
2026-05-14 12:44:30 -04:00
"bowling",
"lmao",
"john"
2026-05-14 11:15:30 -04:00
];
// Manage domains
router.get('/domains', authMw.AllowIfAuthenticated, async (req, res) => {
const result = await dbConnection.transaction(async(t) => {
const ownedDomains = await database.models.RegisteredDomain.findAll({
where: {
owner: req.session.userId
}
}, {transaction: t});
return ownedDomains;
});
let registeredDomains = result;
res.render('domains', {title: 'Domain Manager | Your Domains', registeredDomains: registeredDomains});
});
//// Register domains ////
// GET
// Frontend page to register a new domain
router.get('/domains/new', authMw.AllowIfAuthenticated, async(req, res) => {
res.render('newdomain', {title: 'Domain Manager | Register Domain', supportedTLDs: supportedTLDs});
});
// POST
// Backend post handler to register the domain
router.post('/domains/new', authMw.AllowIfAuthenticated, async(req, res, next) => {
const reqBody = req.body;
const validationResult = domainValidator.test(reqBody);
const validationError = validationResult.error;
let errors = [];
2026-05-14 12:44:30 -04:00
// Make sure domain is valid
const domainLabel = reqBody.register_domain_label;
const domainTLD = reqBody.register_domain_tld;
const fullDomain = domainLabel + domainLabel;
2026-05-14 11:15:30 -04:00
if(validationError !== undefined)
errors = validationError.details;
2026-05-14 12:44:30 -04:00
if(blacklistedDomains.includes(fullDomain.toLowerCase())) {
errors.push('Domain is not available.');
}
2026-05-14 11:15:30 -04:00
try {
if(errors.length === 0) {
const result = await dbConnection.transaction(async(t) => {
const newDomain = await database.models.RegisteredDomain.create({
domain: reqBody.register_domain_label,
tld: reqBody.register_domain_tld,
owner: req.session.userId
}, {transaction: t});
return newDomain;
});
if(result !== undefined) {
return res.redirect('/domains');
} else {
errors.push({message: 'Failed to register new domain.'})
}
}
} catch(error) {
if(error instanceof Sequelize.UniqueConstraintError) {
errors.push({message: 'Domain is not available.'});
} else {
error.status = 500;
return next(error);
}
}
// if we're here we failed, I specify true for csrfToken to force reset it
return res.render('newdomain', {title: pageTitle, supportedTLDs: supportedTLDs, errors: errors, csrfToken: req.csrfToken(true) });
});
module.exports = router;