Add functionality to edit accounts
parent
1901f33851
commit
06765455f6
|
@ -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 hash = bcrypt.hashSync(password, salt);
|
||||
const hash = await generateHash(password);
|
||||
|
||||
const query = `INSERT INTO accounts.users(email, password, admin)
|
||||
VALUES($1, $2, $3)`;
|
||||
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() {
|
||||
const query = `SELECT user_id, email, admin
|
||||
FROM accounts.users
|
||||
|
@ -84,7 +107,8 @@ async function getFromID(id) {
|
|||
return new User(id, row[1], row[2]);
|
||||
}
|
||||
|
||||
exports.createUser = createUser;
|
||||
exports.create = create;
|
||||
exports.edit = edit;
|
||||
exports.retrieveAll = retrieveAll;
|
||||
exports.getFromID = getFromID;
|
||||
exports.passport = passport;
|
|
@ -27,7 +27,21 @@ async function Initialize() {
|
|||
deleteButton.disabled = false;
|
||||
}
|
||||
emailTextbox.disabled = false;
|
||||
emailTextbox.addEventListener('keyup', checkDataValidity);
|
||||
passwordTextbox.disabled = false;
|
||||
passwordTextbox.addEventListener('keyup', checkDataValidity);
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ var sports = require('../database/scores/sports');
|
|||
var divisions = require('../database/scores/divisions');
|
||||
var genders = require('../database/scores/genders');
|
||||
var teams = require('../database/scores/teams');
|
||||
var accounts = require('../database/accounts/accounts');
|
||||
|
||||
function userLoggedIn(req, res, next) {
|
||||
if (req.user) {
|
||||
|
@ -143,4 +144,15 @@ router.get('/account', adminLoggedIn, (req, res, next) => {
|
|||
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;
|
||||
|
|
|
@ -7,7 +7,7 @@ block stylesheets
|
|||
block content
|
||||
div#mobile-view
|
||||
h1 #{title}
|
||||
form#submission-form(action='/auth/register', method='POST')
|
||||
form#submission-form(action='/manage/account', method='POST')
|
||||
span(class='form-section')
|
||||
label Email
|
||||
span(class='form-section-input')
|
||||
|
|
Reference in New Issue