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(
{
email: { type: String, unique: true },
password: String,
displayName: String,
},
{ timestamps: true }
)
UserSchema.plugin(PassportLocalMongoose, {
usernameField: 'email',
})
export const UserModel = model('User', UserSchema)