passport icon indicating copy to clipboard operation
passport copied to clipboard

TypeError: LocalStrategy is not a constructor

Open KonyD opened this issue 1 year ago • 2 comments

error message:

C:\Users\User\Desktop\projects\login-system\passport-config.ts:26
  passport.use(new LocalStrategy({ usernameField: "email" }, authenticateUser));
               ^
TypeError: LocalStrategy is not a constructor
    at initialize (C:\Users\User\Desktop\projects\login-system\passport-config.ts:26:16)
    at Object.<anonymous> (C:\Users\User\Desktop\projects\login-system\server.ts:22:1)
    at Module._compile (node:internal/modules/cjs/loader:1275:14)
    at Module.m._compile (C:\Users\User\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\User\AppData\Roaming\npm\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Function.Module._load (node:internal/modules/cjs/loader:972:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at phase4 (C:\Users\User\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:649:14)

code:

const LocalStrategy = require("passport-local").Stragety;
const bctypt = require("bcrypt");

async function initialize(
  passport: any,
  getUserByEmail: any,
  getUserById: any
) {
  const authenticateUser = async (email: any, password: any, done: any) => {
    const user = getUserByEmail(email);
    if (user == null) {
      return done(null, false, { message: "No user with that email" });
    }

    try {
      if (await bcrypt.compare(password, user.password)) {
        return done(null, user);
      } else {
        return done(null, false, { message: "Password incorrect" });
      }
    } catch (e) {
      return done(e);
    }
  };

  passport.use(new LocalStrategy({ usernameField: "email" }, authenticateUser));
  passport.serializeUser((user: any, done: any) => done(null, user.id));
  passport.deserializeUser((id: any, done: any) => {
    return done(null, getUserById(id));
  });
}

module.exports = initialize;

KonyD avatar Apr 29 '23 20:04 KonyD

Stragety

image on your first line of code the spell of Strategy is incorrect

shobhitexe avatar Jun 07 '23 06:06 shobhitexe

After using above solution, it is giving error - Unknown authentication strategy "azuread-openidconnect"

pramodGit avatar Aug 01 '24 08:08 pramodGit