I am needing some assistance with how to use passport.authenticate() when the username has been encrypted using the NPM bcryptjs library.
For example, when I create a user I encrypt the username before using passport.js to create and save the user to MongoDB. Then in my route to log in, I pass in passport.authenticate("local") as the middleware which does all checking against the username and password.
In this scenario the username is actually the encrypted string, is there a way to use the passport.authenticate to check against the passed in username and an encrypted string?
Pieces of my code that make up the registration and login route.
userParam.username = bcrypt.hashSync(userParam.username);
userModel.create(new userModel(userParam), userParam.password , (error) => {
if (error) {
response.Error = error;
return Promise.resolve(response);
}
});
//Save user
passport.authenticate('local')(req, res, function () {
req.session.save((error) => {
if (error) {
response.Error = error;
return Promise.resolve(error);
}
});
});
Login route.
app.get('/api/user/login', passport.authenticate('local'), function (req, res) {
req.session.save((err) => {
if (err) {
return res.json({ message: "Failed to sign in", err });
}
res.json({ status: "Signed In", authenticated: req.isAuthenticated(), user: req.user, session: req.session });
});
});