passport-local-mongoose: createStrategy is not a function / authenticate is not a function passport-local-mongoose: createStrategy is not a function / authenticate is not a function mongoose mongoose

passport-local-mongoose: createStrategy is not a function / authenticate is not a function


Update your user.ts file like this:

import * as mongoose from 'mongoose';import * as passportLocalMongoose from 'passport-local-mongoose';import { PassportLocalSchema } from 'mongoose';const userSchema = new mongoose.Schema({  email: String,  password: String,  displayname: String,  groups: [{    type: mongoose.Schema.Types.ObjectId,    ref: 'Group'  }]});userSchema.plugin(passportLocalMongoose, {  usernameField: 'email'});const User = mongoose.model('User', userSchema as PassportLocalSchema);export default User;

it worked for me.


Sorry for the late answer. I find a way to resolve this.

import mongoose, { PassportLocalSchema  } from 'mongoose';import passportLocalMongoose from 'passport-local-mongoose';const { Schema } = mongoose;const UserSchema = new Schema({});UserSchema.plugin(passportLocalMongoose);const User: mongoose.PassportLocalModel<mongoose.PassportLocalDocument> =  mongoose.model('User', UserSchema as PassportLocalSchema );export default User;


If I can understand correctly, this is merely caused by an unresolved type, and is not a real code error.

To resolve it -

  1. Include the type definition file in your project, which will extend the types in mongoose: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/passport-local-mongoose/index.d.ts

  2. In your app.ts, import the needed types from mongoose (notice that PassportLocalModel is from the definition file above):

import { Document, PassportLocalModel } from "mongoose";

  1. Now you can cast the updated types to User:

(User as PassportLocalModel<Document>).createStrategy()

(User as PassportLocalModel<Document>).authenticate()