From 376fae2a3367d5813d088a29c5a62938bd55f762 Mon Sep 17 00:00:00 2001 From: Mateja Date: Thu, 6 May 2021 21:07:24 +0200 Subject: Boilerplate for login system. --- controllers/user.js | 25 +++++++++++++++++++++++++ models/user.js | 12 ++++++++++++ routes/web.js | 9 +++++++++ views/home.handlebars | 2 +- views/layouts/main.handlebars | 17 +++++++++++++++++ views/login.handlebars | 14 ++++++++++++++ views/new-post.handlebars | 8 +++++++- views/register.handlebars | 26 ++++++++++++++++++++++++++ 8 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 controllers/user.js create mode 100644 models/user.js create mode 100644 views/login.handlebars create mode 100644 views/register.handlebars diff --git a/controllers/user.js b/controllers/user.js new file mode 100644 index 0000000..e25abf8 --- /dev/null +++ b/controllers/user.js @@ -0,0 +1,25 @@ +module.exports = { + + register(req, res) { + console.log(req.body); + res.redirect('/register'); + }, + + login(req, res) { + console.log(req.body); + res.redirect('/login'); + }, + + logout(req, res) { + res.redirect('/'); + }, + + registerView(req, res) { + res.render('register', {title: 'Register', register: true}); + }, + + loginView(req, res) { + res.render('login', {title: 'Login', login: true}); + } + +}; diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..32b0540 --- /dev/null +++ b/models/user.js @@ -0,0 +1,12 @@ +const mongoose = require('mongoose'); + +const userSchema = new mongoose.Schema({ + firstname: {type: String, required: true}, + lastname: {type: String, required: true}, + email: {type: String, required: true}, + password: {type: String, required: true}, +}); + +const userModel = new mongoose.Model('user', userSchema); + +module.exports = userModel; diff --git a/routes/web.js b/routes/web.js index ee07d85..8960bb3 100644 --- a/routes/web.js +++ b/routes/web.js @@ -1,10 +1,19 @@ const express = require('express'); const router = express.Router(); const postController = require('../controllers/post'); +const userController = require('../controllers/user'); router.get('/', postController.index); router.get('/new-post', postController.create); router.post('/new-post', postController.store); router.get('/remove-post/:id', postController.destroy); +router.get('/register', userController.registerView); +router.get('/login', userController.loginView); + +router.post('/register', userController.register); +router.post('/login', userController.login); + +router.get('/logout', userController.logout); + module.exports = router; diff --git a/views/home.handlebars b/views/home.handlebars index 6061e4e..b0b7d34 100644 --- a/views/home.handlebars +++ b/views/home.handlebars @@ -8,7 +8,7 @@
- {{this.text}} + {{{this.text}}}
Remove
diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index 9eccc37..41585e5 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -31,6 +31,23 @@ New Post {{/if}} + + + diff --git a/views/login.handlebars b/views/login.handlebars new file mode 100644 index 0000000..47e2166 --- /dev/null +++ b/views/login.handlebars @@ -0,0 +1,14 @@ +
+

{{ title }}

+
+
+ + +
+
+ + +
+ +
+
diff --git a/views/new-post.handlebars b/views/new-post.handlebars index 19984ee..9e42ba9 100644 --- a/views/new-post.handlebars +++ b/views/new-post.handlebars @@ -7,8 +7,14 @@
- +
+ + diff --git a/views/register.handlebars b/views/register.handlebars new file mode 100644 index 0000000..d18a6b8 --- /dev/null +++ b/views/register.handlebars @@ -0,0 +1,26 @@ +
+

{{ title }}

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
-- cgit v1.2.3