From 04433b55802519cb49752654dcb2ab6b56a9798d Mon Sep 17 00:00:00 2001 From: Vituin <124085647+Vituin@users.noreply.github.com> Date: Tue, 4 Jun 2024 17:54:37 +0200 Subject: [PATCH] no solved --- .env | 3 ++- config/index.js | 15 +++++++++++++++ package.json | 5 ++++- routes/index.js | 17 ++++++++++++++++- views/auth/login.hbs | 1 + views/auth/register.hbs | 12 ++++++++++++ views/index.hbs | 2 +- 7 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 views/auth/login.hbs create mode 100644 views/auth/register.hbs diff --git a/.env b/.env index c0c68b1ca0..52e086f46e 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -PORT=3000 \ No newline at end of file +PORT=3000 +SESSION_SECRET="bad boy" \ No newline at end of file diff --git a/config/index.js b/config/index.js index 4d9ff5c193..b6d9b5b3eb 100644 --- a/config/index.js +++ b/config/index.js @@ -17,6 +17,10 @@ const favicon = require("serve-favicon"); // https://www.npmjs.com/package/path const path = require("path"); +const session = require(`express-session`) + +const MongoStore = require(`connect-mongo`) + // Middleware configuration module.exports = (app) => { // In development environment the app logs @@ -36,4 +40,15 @@ module.exports = (app) => { // Handles access to the favicon app.use(favicon(path.join(__dirname, "..", "public", "images", "favicon.ico"))); + + app.use( + session({ + secret: process.env.SESSION_SECRET || "super hyper secret key", + resave: false, + saveUninitialized: false, + store: MongoStore.create({ + mongoUrl: MONGO_URI, + }), + }) + ); }; diff --git a/package.json b/package.json index 19489d9695..a1a6388fdb 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,12 @@ "dev": "nodemon server.js" }, "dependencies": { + "bcryptjs": "^2.4.3", + "connect-mongo": "^5.1.0", "cookie-parser": "^1.4.5", "dotenv": "^8.2.0", "express": "^4.17.1", + "express-session": "^1.18.0", "hbs": "^4.1.1", "mongoose": "^6.1.2", "morgan": "^1.10.0", @@ -18,4 +21,4 @@ "devDependencies": { "nodemon": "^2.0.7" } -} +} \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 81c2396ceb..9ae9e905ee 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,23 @@ const router = require("express").Router(); +const User = require(`../models/User.model`) +const bcrypt = require(`bcryptjs`) /* GET home page */ router.get("/", (req, res, next) => { - res.render("index"); + res.render(`auth/register`); }); +router.post(`/register`, async (req, res) => { + const salt = await bcrypt.genSalt(10) + const hashedPassword = await bcrypt.hash(req.body.password, salt) + const newUser = { + username: req.body.username, + password: hashedPassword + } + User.create(newUser).then((data) => { + res.render(`index`, { user: data }) + }) + .catch((error) => res.json(error)) +}) + module.exports = router; diff --git a/views/auth/login.hbs b/views/auth/login.hbs new file mode 100644 index 0000000000..1ce1e208bc --- /dev/null +++ b/views/auth/login.hbs @@ -0,0 +1 @@ +
Welcome to {{title}}
+Welcome to {{title}}, {{user.username}}
\ No newline at end of file