【问题标题】:Passport-local-mongoose serializeUser incorrect typePassport-local-mongoose serializeUser 类型不正确
【发布时间】:2021-08-15 22:51:35
【问题描述】:

我正在尝试使用 Passportjs 和 mongoose 进行身份验证,但我很难使用 typescript 获得正确的类型。

Passport.use(UserModel.createStrategy())
Passport.serializeUser(UserModel.serializeUser()) // <---- 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.

这就是我的 User 类的样子:


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)

【问题讨论】:

    标签: node.js typescript mongoose passport.js passport-local-mongoose


    【解决方案1】:

    更新 - my PR landed 所以这个问题已经在v6.1.0@types/passport-local-mongoose 中得到修复。要获得修复,请将此依赖项更新为 6.1.0,然后从您自己的 mongo User 扩展 Express.User 类。因为您的 Mongo User 使用与 Express User 相同的类名,您需要创建一个别名,如下所示。在致电Passport.serializeUser 之前执行此操作,您应该一切顺利。你可以把 Express.User 扩展放在任何你想要的地方,我在我的routes/user.ts 中添加了,因为那是我代码库中最合适的地方。

    type _User = User;
    
    declare global {
      namespace Express {
        interface User extends _User {
        }
      }
    }
    
    Passport.use(UserModel.createStrategy())
    Passport.serializeUser(UserModel.serializeUser()) // <---- No Error
    

    -- 原始答案--

    我相信这是@types/passport-local-mongoose 中的一个错误,我reported today。您可以通过向任何此类添加演员来修复该错误。

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

    【讨论】:

    • 希望他们修复它,我只是使用//@ts-ignore,但我喜欢这种投射它的方法。感谢您报告。
    • 我刚刚也为此登陆了pull request
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2018-12-13
    • 2018-11-07
    • 1970-01-01
    • 2021-10-13
    • 2015-01-16
    相关资源
    最近更新 更多