From 48c2ee67bd9b5cc9e7292c382bb172606e226c40 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Wed, 24 Nov 2021 18:25:27 -0700 Subject: [PATCH] Add page to create user --- app.js | 2 ++ database/accounts/accounts.js | 4 +++- routes/admin.js | 12 ++++++++++++ routes/auth.js | 9 ++++++++- views/accounts/createuser.pug | 20 ++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 routes/admin.js create mode 100644 views/accounts/createuser.pug diff --git a/app.js b/app.js index f8cb47b..3efd585 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,7 @@ var usersRouter = require('./routes/users'); var dataRouter = require('./routes/data'); var manageRouter = require('./routes/manage'); var authRouter = require('./routes/auth'); +var adminRouter = require('./routes/admin'); var app = express(); @@ -49,6 +50,7 @@ app.use('/users', usersRouter); app.use('/data', dataRouter); app.use('/manage', manageRouter); app.use('/auth', authRouter); +app.use('/admin', adminRouter); // catch 404 and forward to error handler diff --git a/database/accounts/accounts.js b/database/accounts/accounts.js index d65de3c..5ae6cc9 100644 --- a/database/accounts/accounts.js +++ b/database/accounts/accounts.js @@ -48,4 +48,6 @@ async function createUser(email, password) { const query = `INSERT INTO accounts.users(email, password) VALUES($1, $2)`; await database.executeQuery(query, [email, hash]); -} \ No newline at end of file +} + +exports.createUser = createUser; \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js new file mode 100644 index 0000000..d032cd1 --- /dev/null +++ b/routes/admin.js @@ -0,0 +1,12 @@ +var express = require('express'); +var router = express.Router(); +const passport = require('passport'); +const app = require('../app'); +const accounts = require('./../database/accounts/accounts'); + + +router.get('/createuser', (req, res, next) => { + res.render('accounts/createuser', { title: 'Create user' }); +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js index 5a72c2f..38426ea 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,5 +1,7 @@ -const passport = require('passport'); +var express = require('express'); var router = express.Router(); +const passport = require('passport'); +const accounts = require('./../database/accounts/accounts'); const app = require('../app'); router.post('/login', passport.authenticate('local'), (req, res, next) => { @@ -8,4 +10,9 @@ router.post('/login', passport.authenticate('local'), (req, res, next) => { res.json(user); }); +router.post('/register', (req, res, next) => { + accounts.createUser(req.body.email, req.body.password) + .then(res.redirect('/')); +}); + module.exports = router; \ No newline at end of file diff --git a/views/accounts/createuser.pug b/views/accounts/createuser.pug new file mode 100644 index 0000000..c5630c8 --- /dev/null +++ b/views/accounts/createuser.pug @@ -0,0 +1,20 @@ +extends ../layout + +block stylesheets + link(rel='stylesheet', href='/stylesheets/submit.css') + link(rel='stylesheet', href='/stylesheets/form.css') + +block content + div#mobile-view + h1 #{title} + form(action='/auth/register', method='POST') + span(class='form-section') + label Email + span(class='form-section-input') + input(type="email", name="email") + span(class='form-section') + label Password + span(class='form-section-input') + input(type="password", name="password") + span(class='form-section') + button#submit-button(type="submit") Submit \ No newline at end of file