diff --git a/routes/admin.js b/routes/admin.js index b9509e0..28526e0 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -90,6 +90,63 @@ router.post('/admin/api/v1/register', adminUserMiddleware.adminAuthenticationReq }); }); +/* +* /admin/api/v1/removeadmin +* - requires admin auth +* +* registers a new admin user +* +* post { +* id - id of the admin user +* } +* return { +* code: httpcode +* success: boolean - true if delete was successull +* errors: Strings[messages] +* } +*/ +router.post('/admin/api/v1/removeadmin', adminUserMiddleware.adminAuthenticationRequired, (req, res) => { + if (!req.body) { + common.sendApiGenericError(res); + return; + } + + const { id } = req.body; + adminUser.adminUserModel.findByIdAndDelete(id, (err) => { + if (err) return common.sendApiError(res, 500, [err]); + // successfull + common.sendApiReturn(res, {}); + }); +}); + +/* +* /admin/api/v1/listadmins +* - requires admin auth +* +* gets list of admins +* +* return { +* code: httpcode +* success: boolean - true if delete was successull +* errors: Strings[messages] +* } +*/ +router.get('/admin/api/v1/listadmins', adminUserMiddleware.adminAuthenticationRequired, (req, res) => { + adminUser.adminUserModel.find({}, (err, admins) => { + // TODO format exception so it doesnt have a huge list of errors + if (err) return common.sendApiError(res, 500, [err]); + + const output = []; + for (let i = 0, l = admins.length; i < l; i++) { + admins[i].password = undefined; + output.push(admins[i]); + } + common.sendApiReturn(res, { + admins: output + }); + }); +}); + /* * /admin/api/v1/check * diff --git a/setupAdminUser.js b/setupAdminUser.js new file mode 100644 index 0000000..3b4fe24 --- /dev/null +++ b/setupAdminUser.js @@ -0,0 +1,12 @@ +const adminUser = require('./models/admin-user'); + +const newUser = new adminUser.adminUserModel({ + username: 'admin', + password: 'root' +}); + +newUser.save().then(() => { + console.log('success, user admin pass root'); +}).catch((rejection) => { + console.log(rejection); +}); \ No newline at end of file diff --git a/views/admin.hbs b/views/admin.hbs index 50d192a..c328c18 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -20,6 +20,13 @@ +

delete admin

+
+

id

+ + +
+

create blog post

content

@@ -102,6 +109,8 @@
+
+ List of admin users
List of blog posts