I am using this tutorial to learn PassportJS. I am storing their data in MongoDB and testing with Postman Desktop. I am sending a post request to /register with email and password fields. When the email exists, the request executes and I get the expected response: Email is already taken. However, when the email is unique, the request will hang, and Postman will get stuck on Sending Request message without displaying any error.
Ex. sending email: test and password: test, the terminal indicates POST /register?email=test&password=test 302 58.162 ms - 30, and Postman continuously loads Sending Request. At this point, nothing has changed in the database, however, when I cancel the request, the entry is successfully added to the database. Finally, the terminal displays GET /profile - - ms - -
Here is the code of relevance:
user.js
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const userSchema = mongoose.Schema({
email: {
type: String,
unique: true,
},
password: {
type: String,
},
});
userSchema.methods.generateHash = (password) => {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = (password) => {
return bcrypt.compareSync(password, this.password);
};
module.exports = mongoose.model("User", userSchema);
routes.js
module.exports = (app, passport) => {
app.get("/", (req, res) => {
res.json("Welcome to nodeJS auth App. Please register/login");
});
app.get("/register", (req, res) => {
res.json({ message: req.flash("registerMessage") });
});
app.post(
"/register",
passport.authenticate("register", {
successRedirect: "/profile",
successFlash: true,
failureRedirect: "/register",
failureFlash: true,
})
);
...
passport.js
const LocalStrategy = require("passport-local").Strategy;
const User = require("../models/user");
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(
"register",
new LocalStrategy(
{
usernameField: "email",
passwordField: "password",
passReqToCallback: true,
},
function (req, email, password, done) {
User.findOne({ email: email }, function (err, user) {
if (err) {
return done(err);
}
if (user) {
return done(
null,
false,
req.flash("registerMessage", "Email is already taken.")
);
} else {
const newUser = new User();
newUser.email = email;
newUser.password = newUser.generateHash(password);
newUser.save(function (err) {
if (err) throw err;
return done(null, newUser);
});
}
});
}
)
);
...
server.js
const express = require("express"),
app = express();
const morgan = require("morgan");
const flash = require("connect-flash");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const passport = require("passport");
const config = require("./config/database");
const port = process.env.PORT || 3000;
require("./config/passport")(passport);
app.use(express.json());
app.use(express.urlencoded({ urlencoded: true, extended: false }));
app.use(cookieParser());
app.use(morgan("dev"));
app.use(
session({
secret: "secret123",
saveUninitialized: true,
resave: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
require("./routes/routes")(app, passport);
app.listen(port, () => {
console.log("Server on port " + port);
});
So far, I have checked out at least 10 Stack Overflow links with similar issues, and here's what I tried:
- Reinstalling Postman
- Adding
process.nextTick()outside of the register function in passport.js console.log()debug- close the database connection before calling the
done()function - changed const to var inside of function
- add
(req,res) => {}to thePOST /registerroute with a response line.
What I have found throughout this debugging process is that there may be some issue with passing newUser into done() in passport.js, this could also be leading to an issue with the serializeUser function. Note that most of the Stack Overflow issues I was looking at were from over 2 years ago, which could be contributing to the lack of proper functionality when following their advice.
If you take the time to answer this, please provide me with feedback on how I posed this question. I would like to get better at asking questions on this site.