passport-local-mongoose icon indicating copy to clipboard operation
passport-local-mongoose copied to clipboard

Typescript issues.

Open jacobcoro opened this issue 4 years ago • 5 comments

I'm also experiencing the typescript issues mentioned below. I tried npm installing all the @types/ but that didn't help.

I still have this issue with typescript, doesn't compile because register is not a member of the User model. any ideas?

The strange this is when I run your project it works but when I import the same code in my project it doesn't work - I'm new to typescript so not sure of what's the usual steps to undertake when this sort of collision occurs.

Originally posted by @hajjboy95 in https://github.com/saintedlama/passport-local-mongoose/issues/229#issuecomment-462678409

jacobcoro avatar Jul 02 '20 05:07 jacobcoro

@Jewcub check this link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/passport-local-mongoose/passport-local-mongoose-tests.ts.

I was facing same error and resolved it using the above link.

harsimrandev avatar Jul 14 '20 12:07 harsimrandev

same problem as #229 , temporarily using (User as any).register(), I don't really want that. someone please fix typings.

Sparkenstein avatar Dec 25 '20 15:12 Sparkenstein

Solved:

import { Document, PassportLocalDocument, PassportLocalModel, PassportLocalSchema } from "mongoose";

interface User extends PassportLocalDocument {
  username: string;
  password: string;
}

const UserSchema = new Schema(
  {
    username: String,
    password: String,
  }) as PassportLocalSchema;

interface UserModel <T extends Document> extends PassportLocalModel<T> {}

UserSchema.plugin(passportLocalMongoose);

const UserModel: UserModel<User> = mongoose.model<User>("user", UserSchema);

This should give you all the passport local methods like .registered() on usermodel

Sparkenstein avatar Dec 26 '20 05:12 Sparkenstein

Same problems, very confusing and annoying 😕

on3dd avatar Jan 23 '21 06:01 on3dd