Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PORT=3000
PORT=3000
SESSION_SECRET="bad boy"
15 changes: 15 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}),
})
);
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -18,4 +21,4 @@
"devDependencies": {
"nodemon": "^2.0.7"
}
}
}
17 changes: 16 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions views/auth/login.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2>Login</h2>
12 changes: 12 additions & 0 deletions views/auth/register.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h2>Register</h2>

<form action="/register" method="POST">
<label for="username">Username</label>
<input type="text" , id="username" , name="username">

<label for="password">Password</label>
<input type="password" , id="password" , name="password">

<button type="submit">Register</button>

</form>
2 changes: 1 addition & 1 deletion views/index.hbs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>{{title}}</h1>
<p>Welcome to {{title}}</p>
<p>Welcome to {{title}}, {{user.username}}</p>