saintedlama / passport-local-mongoose

Passport-Local Mongoose is a Mongoose plugin that simplifies building username and password login with Passport
MIT License
1.17k stars 295 forks source link

Typescript issues. #304

Open jacobcoro opened 4 years ago

jacobcoro commented 4 years ago

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

harsimrandev commented 4 years ago

@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.

Sparkenstein commented 3 years ago

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

Sparkenstein commented 3 years ago

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

on3dd commented 3 years ago

Same problems, very confusing and annoying 😕

pavelsvitek commented 1 month ago

4 years and this is still not in README 👀