Advanced Node and Express - Hashing Your Passwords
Hi, I get the following message after run the test:
You should use hash the password in the registration.
On glitch I have not any error, and the registration-login-logout works fine as expected.
Here is my code:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require("express-session");
const passport = require("passport");
const mongo = require("mongodb").MongoClient;
const ObjectID = require("mongodb").ObjectID;
const LocalStrategy = require("passport-local");
const bcrypt = require("bcrypt");
const app = express();
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "pug");
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
mongo.connect(process.env.DATABASE, (err, db) => {
if (err) {
console.log("Database error:", err);
} else {
console.log("Successful database connection.");
const dbo = db.db("cluster0-l7ww6");
// serialisation and app listen...
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
dbo.collection("users").findOne({_id: new ObjectID(id)}, (err, doc) => {
done(null, doc );
});
});
passport.use(new LocalStrategy((username, password, done) => {
dbo.collection("users").findOne({username: username}, (err, user) => {
console.log("User", username, "attempted to log in.");
if (err) return done(err);
if (!(user)) return done(null, false);
if (!(bcrypt.compareSync(password, user.password))) return done(null, false);
return done(null, user);
});
}));
const ensureAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) return next();
res.redirect("/");
};
app.route('/')
.get((req, res) => {
res.render(__dirname + "/views/pug/index.pug", {
title: "Home Page ",
message: "Please login",
showLogin: true,
showRegistration: true
});
})
;
app.route("/login")
.post(passport.authenticate("local", {failureRedirect: "/"}), (req, res) => {
res.redirect("/profile");
});
app.route("/profile")
.get(ensureAuthenticated, (req, res) => {
res.render(__dirname + "/views/pug/profile.pug", {
username: req.user.username
});
});
app.route("/register")
.post((req, res, next) => {
dbo.collection("users").findOne({username: req.body.username}, (err, user) => {
if (err) {
next(err);
} else if (user) {
res.redirect("/");
} else {
const hash = bcrypt.hashSync(req.body.password, 8)
dbo.collection("users").insertOne({
username: req.body.username,
password: hash
}, (err, doc) => {
if (err) {
res.redirect("/");
} else {
next(null, user);
}
});
}
});
}, passport.authenticate("local", {failureRedirect: "/"}), (req, res, next) => {
res.redirect("/profile");
});
app.route("/logout")
.get((req, res) => {
req.logout();
res.redirect("/");
});
// handling missing page(404):
app.use((request, response, next) => {
response.status(404)
.type("text")
.send("Not found");
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}
});