I keep getting the Error: Unknown authentication strategy "local" error.
This is a bit of my main file (server.js):
...
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
...
// Initialize passport
const initializePassport = require("./passport-config");
initializePassport(
passport,
(email) => searchUser({ email: email }),
(id) => searchUser({ id: id })
);
...
// Post request - Login user
app.post(
"/login",
checkNotAuthenticated,
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true,
})
);
this is my passport config file (passport-config.js):
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = getUserByEmail(email);
if (user == null) {
return done(null, false, { message: "No user with that email" });
}
bcrypt.compare(password, user.password, function (err, result) {
if (e) {
return done(e);
}
if (result) {
return done(null, user);
} else {
return done(null, false, { message: "Password incorrect" });
}
});
passport.use(
new LocalStrategy({ usernameField: "email" }, authenticateUser)
);
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser((id, done) => {
return done(null, getUserById(id));
});
};
}
module.exports = initialize;
i can't figure out whats the issue here, i'm also pretty new to passport, node and express this is not my code but i took it from a tutorial in a yt video that didn't (somehow) work, so i tried making it work, i also was trying to add mongodb storage but i dont think thats the issue