V
V
Vann Damm2020-10-21 12:47:49
JavaScript
Vann Damm, 2020-10-21 12:47:49

PassportJS. ERROR Unknown authentication strategy "local"?

PassportJS - throws an error ERROR Unknown authentication strategy "local"
Everything has already been chordcoded, but it is still present. What could be the problem?

const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcryptjs");

const handleTest = async (app) => {
  try {
    app.use(passport.initialize());
    passport.use(
      {
        usernameField: "email",
        passwordField: "password",
      },
      new LocalStrategy(async (req, email, password, done) => {
        try {
          console.log(email);
          const user = await UserModel.findOne({
            email: email,
          }).exec();
          if (user) {
            const isMatch = await bcrypt.compare(password, user.password);
            if (isMatch) {
              return done(null, user);
            }
            return done(null, false);
          }
          return done(null, false);
        } catch (error) {
          return done(error, false);
        }
      })
    );
    
    app.post(
      `/auth/login`,
      passport.authenticate("local", { session: false }),
      async (req, res) => {
        try {
          const user = req.user;
          let token = jwt.sign(
            { userId: user.id },
            process.env.SECRET_KEY || "fd?2??7)!!FdDD",
            {
              expiresIn: "15d",
            }
          );
          token = {
            status: "success",
            data: {
              token,
              userId: user.id,
            },
          };
          console.log(token);
          res.send(token);
        } catch (error) {
          throw new Error(error);
        }
      }
    );
  } catch (error) {
    throw new Error(error);
  }
};

module.exports = handleTest;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question