diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index ef0d040..ce0be9f 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -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; \ No newline at end of file diff --git a/public/scripts/manage/account.js b/public/scripts/manage/account.js index 6b51c2f..65c9f4e 100644 --- a/public/scripts/manage/account.js +++ b/public/scripts/manage/account.js @@ -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(); \ No newline at end of file +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; +} + diff --git a/routes/manage.js b/routes/manage.js index e9d4377..9e038f0 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -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; diff --git a/views/accounts/createuser.pug b/views/accounts/createuser.pug index cc5324d..36e0acb 100644 --- a/views/accounts/createuser.pug +++ b/views/accounts/createuser.pug @@ -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')