101 lines
3.1 KiB
JavaScript
101 lines
3.1 KiB
JavaScript
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';
|
|
|
|
const blacklistedDomains = [
|
|
"dns.internal"
|
|
];
|
|
|
|
const supportedTLDs = [
|
|
"internal",
|
|
"tomato",
|
|
"secret",
|
|
"money",
|
|
"lol",
|
|
"bowling",
|
|
"lmao",
|
|
"john"
|
|
];
|
|
|
|
// 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 = [];
|
|
|
|
// Make sure domain is valid
|
|
const domainLabel = reqBody.register_domain_label;
|
|
const domainTLD = reqBody.register_domain_tld;
|
|
const fullDomain = domainLabel + domainLabel;
|
|
|
|
if(validationError !== undefined)
|
|
errors = validationError.details;
|
|
|
|
if(blacklistedDomains.includes(fullDomain.toLowerCase())) {
|
|
errors.push('Domain is not available.');
|
|
}
|
|
|
|
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; |