diff --git a/README.md b/README.md index 38a2b8e..aa462a5 100755 --- a/README.md +++ b/README.md @@ -54,9 +54,12 @@ Example: retirementCalculator/ ├── src/ │ ├── views/ -| ├── css/ -| ├── images/ -| └── scripts/ +│ │ └── partials/ +│ ├── css/ +│ ├── images/ +│ ├── scripts/ +│ └── utils/ +│ ├── app.js ├── .env.example ├── .gitignore diff --git a/app.js b/app.js index cc6a71b..8b997a5 100644 --- a/app.js +++ b/app.js @@ -72,7 +72,7 @@ app.get('/login', (req, res) => { }); app.get('/home', (req, res) => { - res.render('home'); + res.render('home', { session: req.session }); return res.status(status.Ok); }); @@ -106,6 +106,11 @@ app.get('/settings', (req, res) => { return res.status(status.Ok); }); +app.get('/logout', (req, res) => { + req.session.destroy(); + return res.redirect('/login'); +}); + app.get('/*splat', (req, res) => { res.send('404 Not Found'); return res.status(status.NotFound); diff --git a/src/auth/authentication.js b/src/auth/authentication.js index 2a2e9ed..f22b764 100644 --- a/src/auth/authentication.js +++ b/src/auth/authentication.js @@ -27,7 +27,7 @@ module.exports = (users) => { return res.redirect("/login"); } - if (!bcrypt.compare(req.body.password, user.password)) { + if (!bcrypt.compareSync(req.body.password, user.password)) { req.session.errMessage = "Incorrect password"; res.status(status.Unauthorized); return res.redirect("/login"); @@ -64,7 +64,7 @@ module.exports = (users) => { return res.redirect("/signup"); } - let hashedPassword = await bcrypt.hash(req.body.password, salt); + let hashedPassword = await bcrypt.hashSync(req.body.password, salt); users.insertOne({ email: req.body.email, diff --git a/src/router/user.js b/src/router/user.js index 5570874..3a2cdee 100644 --- a/src/router/user.js +++ b/src/router/user.js @@ -31,5 +31,10 @@ module.exports = (middleware) => { return res.status(status.Ok); }); + router.get('/logout', middleware, (req, res) => { + req.session.destroy(); + return res.redirect('/login'); + }); + return router; }; diff --git a/src/views/home.ejs b/src/views/home.ejs index 3bb7034..b354997 100644 --- a/src/views/home.ejs +++ b/src/views/home.ejs @@ -10,6 +10,7 @@
<%- include("./partials/header") %> + <% if (session.authenticated) { console.log(session.email); } else { console.log("No user"); } %>