Add functionality to edit accounts

main
sudoer777 2021-11-25 12:40:19 -07:00
parent 1901f33851
commit 06765455f6
4 changed files with 55 additions and 5 deletions

View File

@ -52,16 +52,39 @@ passport.deserializeUser((id, cb) => {
}); });
async function generateHash(password) {
return bcrypt.hashSync(password, salt);
}
async function createUser(email, password, isAdmin) { async function create(email, password, isAdmin) {
const salt = bcrypt.genSaltSync(); const salt = bcrypt.genSaltSync();
const hash = bcrypt.hashSync(password, salt); const hash = await generateHash(password);
const query = `INSERT INTO accounts.users(email, password, admin) const query = `INSERT INTO accounts.users(email, password, admin)
VALUES($1, $2, $3)`; VALUES($1, $2, $3)`;
await database.executeQuery(query, [email, hash, isAdmin]); await database.executeQuery(query, [email, hash, isAdmin]);
} }
async function edit(id, email, password, isAdmin) {
if(password) {
const hash = await generateHash(password);
const query = `UPDATE accounts.users
SET email = $2,
password = $3,
admin = $4
WHERE user_id = $1;`;
await database.executeQuery(query, [id, email, hash, isAdmin]);
} else {
const query = `UPDATE accounts.users
SET email = $2,
admin = $3
WHERE user_id = $1;`;
await database.executeQuery(query, [id, email, isAdmin]);
}
return new User(id, email, isAdmin);
}
async function retrieveAll() { async function retrieveAll() {
const query = `SELECT user_id, email, admin const query = `SELECT user_id, email, admin
FROM accounts.users FROM accounts.users
@ -84,7 +107,8 @@ async function getFromID(id) {
return new User(id, row[1], row[2]); return new User(id, row[1], row[2]);
} }
exports.createUser = createUser; exports.create = create;
exports.edit = edit;
exports.retrieveAll = retrieveAll; exports.retrieveAll = retrieveAll;
exports.getFromID = getFromID; exports.getFromID = getFromID;
exports.passport = passport; exports.passport = passport;

View File

@ -27,7 +27,21 @@ async function Initialize() {
deleteButton.disabled = false; deleteButton.disabled = false;
} }
emailTextbox.disabled = false; emailTextbox.disabled = false;
emailTextbox.addEventListener('keyup', checkDataValidity);
passwordTextbox.disabled = false; passwordTextbox.disabled = false;
passwordTextbox.addEventListener('keyup', checkDataValidity);
adminCheckbox.disabled = false; adminCheckbox.disabled = false;
checkDataValidity();
} }
Initialize(); Initialize();
async function checkDataValidity() {
let dataIsValid = true;
if(!passwordTextbox.value && !passwordTextbox.placeholder) dataIsValid = false;
if(!emailTextbox.value) dataIsValid = false;
if(dataIsValid) submitButton.disabled = false;
else submitButton.disabled = true;
}

View File

@ -7,6 +7,7 @@ var sports = require('../database/scores/sports');
var divisions = require('../database/scores/divisions'); var divisions = require('../database/scores/divisions');
var genders = require('../database/scores/genders'); var genders = require('../database/scores/genders');
var teams = require('../database/scores/teams'); var teams = require('../database/scores/teams');
var accounts = require('../database/accounts/accounts');
function userLoggedIn(req, res, next) { function userLoggedIn(req, res, next) {
if (req.user) { if (req.user) {
@ -143,4 +144,15 @@ router.get('/account', adminLoggedIn, (req, res, next) => {
res.render('accounts/createuser', { title }); res.render('accounts/createuser', { title });
}); });
router.post('/account', adminLoggedIn, (req, res, next) => {
const email = req.body.email;
const password = req.body.password;
const isAdmin = !!req.body.admin;
const accountID = req.body.account;
if(accountID) accounts.edit(accountID, email, password, isAdmin).then(res.redirect('/manage'));
else accounts.create(req.body.email, req.body.password, !!req.body.admin).then(res.redirect('/manage'));
});
module.exports = router; module.exports = router;

View File

@ -7,7 +7,7 @@ block stylesheets
block content block content
div#mobile-view div#mobile-view
h1 #{title} h1 #{title}
form#submission-form(action='/auth/register', method='POST') form#submission-form(action='/manage/account', method='POST')
span(class='form-section') span(class='form-section')
label Email label Email
span(class='form-section-input') span(class='form-section-input')