86 lines
2.7 KiB
JavaScript
86 lines
2.7 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 supportedTLDs = [
|
||
|
|
"local",
|
||
|
|
"tomato",
|
||
|
|
"secret",
|
||
|
|
"money",
|
||
|
|
"lol",
|
||
|
|
"lmao"
|
||
|
|
];
|
||
|
|
|
||
|
|
// 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 = [];
|
||
|
|
|
||
|
|
if(validationError !== undefined)
|
||
|
|
errors = validationError.details;
|
||
|
|
|
||
|
|
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;
|