Improve error handling for account management
parent
d2d6bbc514
commit
5c783880a7
|
@ -156,16 +156,16 @@ router.get('/account', userLoggedIn, (req, res, next) => {
|
||||||
if(userIsAdmin) {
|
if(userIsAdmin) {
|
||||||
let title = req.query.account ? 'Manage User' : 'Create User'
|
let title = req.query.account ? 'Manage User' : 'Create User'
|
||||||
|
|
||||||
res.render('accounts/createuser', { title, userLoggedIn: !!req.user });
|
res.render('accounts/createuser', { title, userLoggedIn: !!req.user, message: req.flash('error') });
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let title = 'Manage Account';
|
let title = 'Manage Account';
|
||||||
|
|
||||||
res.render('accounts/createuser', { title, accountID, userLoggedIn: !!req.user });
|
res.render('accounts/createuser', { title, accountID, userLoggedIn: !!req.user, message: req.flash('error') });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/account', userLoggedIn, (req, res, next) => {
|
router.post('/account', userLoggedIn, async function(req, res, next) {
|
||||||
const email = req.body.email;
|
const email = req.body.email;
|
||||||
const password = req.body.password;
|
const password = req.body.password;
|
||||||
|
|
||||||
|
@ -175,19 +175,24 @@ router.post('/account', userLoggedIn, (req, res, next) => {
|
||||||
const loggedInAccountIsAdmin = req.user[2];
|
const loggedInAccountIsAdmin = req.user[2];
|
||||||
const loggedInAccountID = req.user[0];
|
const loggedInAccountID = req.user[0];
|
||||||
|
|
||||||
console.log(accountID);
|
|
||||||
console.log(loggedInAccountID);
|
|
||||||
|
|
||||||
|
|
||||||
if(!loggedInAccountIsAdmin && accountID != loggedInAccountID) {
|
if(!loggedInAccountIsAdmin && accountID != loggedInAccountID) {
|
||||||
res.status(403).send("ACCESS DENIED");
|
res.status(403).send("ACCESS DENIED");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const isAdmin = loggedInAccountIsAdmin ? !!req.body.admin : false;
|
try {
|
||||||
|
const isAdmin = loggedInAccountIsAdmin ? !!req.body.admin : false;
|
||||||
|
|
||||||
if(remove) accounts.remove(accountID).then(res.redirect('/manage'));
|
if(remove) await accounts.remove(accountID);
|
||||||
if(accountID) accounts.edit(accountID, email, password, isAdmin).then(res.redirect('/manage'));
|
else if(accountID) await accounts.edit(accountID, email, password, isAdmin);
|
||||||
else accounts.create(req.body.email, req.body.password, !!req.body.admin).then(res.redirect('/manage'));
|
else await accounts.create(req.body.email, req.body.password, !!req.body.admin);
|
||||||
|
|
||||||
|
res.redirect('/manage');
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error("ERROR: " + err.message);
|
||||||
|
req.flash("error", "An error has occurred.");
|
||||||
|
res.redirect('/manage/account');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ block content
|
||||||
span(class='form-section-checkbox')
|
span(class='form-section-checkbox')
|
||||||
input#admin-checkbox(type="checkbox" name="admin" disabled)
|
input#admin-checkbox(type="checkbox" name="admin" disabled)
|
||||||
label(for="admin-checkbox") Grant admin privileges
|
label(for="admin-checkbox") Grant admin privileges
|
||||||
|
.error #{message}
|
||||||
span(class='form-section')
|
span(class='form-section')
|
||||||
button#submit-button(type="submit" disabled) Submit
|
button#submit-button(type="submit" disabled) Submit
|
||||||
span(class='form-section')
|
span(class='form-section')
|
||||||
|
|
Reference in New Issue