Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: LocalStrategy is not a constructor #979

Open
KonyD opened this issue Apr 29, 2023 · 1 comment
Open

TypeError: LocalStrategy is not a constructor #979

KonyD opened this issue Apr 29, 2023 · 1 comment

Comments

@KonyD
Copy link

KonyD commented Apr 29, 2023

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;
@shobhitexe
Copy link

Stragety

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants