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

Type issues with SerializeUser #332

Open CarlosAMendoza opened 3 years ago

CarlosAMendoza commented 3 years ago

I'm trying to use Passportjs, and mongoose for authentication but I am having a hard time getting the correct type with typescript.

Passport.use(UserModel.createStrategy())
Passport.serializeUser(UserModel.serializeUser()) // <---- Error

I get the error:

No overload matches this call.
  Overload 1 of 2, '(fn: (user: User, done: (err: any, id?: any) => void) => void): void', gave the following error.
    Argument of type '(user: PassportLocalModel<User>, cb: (err: any, id?: any) => void) => void' is not assignable to parameter of type '(user: User, done: (err: any, id?: any) => void) => void'.
      Types of parameters 'user' and 'user' are incompatible.
        Type 'User' is missing the following properties from type 'PassportLocalModel<User>': authenticate, serializeUser, deserializeUser, register, and 68 more.
  Overload 2 of 2, '(fn: (req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error.
    Argument of type '(user: PassportLocalModel<User>, cb: (err: any, id?: any) => void) => void' is not assignable to parameter of type '(req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void'.
      Types of parameters 'user' and 'req' are incompatible.
        Type 'IncomingMessage' is missing the following properties from type 'PassportLocalModel<User>': authenticate, serializeUser, deserializeUser, register, and 53 more.

This is what my User class looks like:


export interface User extends Document {
  email: String
  password: String
  displayName: String
}

const UserSchema = new Schema(
  {
    username: { type: String, unique: true },
    password: String,
    displayName: String,
    roles: [{ type: String }],
  },
  { timestamps: true }
)

UserSchema.plugin(PassportLocalMongoose)

export const UserModel = model<User>('User', UserSchema)
dracuusta commented 2 weeks ago

I'm experiencing the same issue. Did you find a solution? 1. No overload matches this call. Overload 1 of 4, '(fn: (user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error. Argument of type '(user: IUser, done: (err: any, id?: unknown) => void) => void' is not assignable to parameter of type '(user: User, done: (err: any, id?: unknown) => void) => void'. Types of parameters 'user' and 'user' are incompatible. Type 'User' is missing the following properties from type 'IUser': id, first_name, last_name, username, and 3 more. Overload 2 of 4, '(fn: (req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error. Argument of type '(user: IUser, done: (err: any, id?: unknown) => void) => void' is not assignable to parameter of type '(req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void'. Types of parameters 'user' and 'req' are incompatible. Type 'IncomingMessage' is missing the following properties from type 'IUser': id, first_name, last_name, username, and 3 more.