Loopback 4 绑定 USER_SERVICE 与自定义用户服务

Loopback 4 binding USER_SERVICE with a custom user service

我正在尝试使用实现 UserService<MyUser, MyCredentials>:

的自定义 MyUserService
export class MyUserService implements UserService<Account, LoginCredential> { ... }

与环回的 UserCredentials 的区别是:

问题是,当我尝试绑定(在 application.ts 中)时:

this.bind(UserServiceBindings.USER_SERVICE).toClass(MyUserService);

由于以下原因无法正常工作:

Argument of type 'typeof MyUserService' is not assignable to parameter of type 'Constructor<UserService<User, Credentials>>'.
  Types of construct signatures are incompatible.
    Type 'new (myUserRepository: MyUserRepository, passwordHasher: PasswordHasher<string>) => MyUserService' is not assignable to type 'new (...args: any[]) => UserService<User, Credentials>'.
      Construct signature return types 'MyUserService' and 'UserService<User, Credentials>' are incompatible.
        The types of 'verifyCredentials' are incompatible between these types.
          Type '(myCredentials: MyCredentials) => Promise<MyUser>' is not assignable to type '(credentials: Credentials) => Promise<User>'.
            Types of parameters 'myCredentials' and 'credentials' are incompatible.
              Property 'identity' is missing in type 'Credentials' but required in type 'MyCredentials'.ts(2345)
myuser.repository.ts(8, 3): 'identity' is declared here.

换句话说,它不允许我绑定我的自定义 UserService,除非我遵循这些规则:

我的问题是:

如何绑定我的自定义用户服务,我可以在其中自定义存储在 credentials 中的数据?

问题比我想象的要简单。长话短说:

只需确保我正在导入 我自己的 UserServiceBindings 而不是 Loopback 的默认 UserServiceBindings.


就我而言,我在 application.ts 中的 UserServiceBindings 如下:

import {UserServiceBindings} from '@loopback/authentication-jwt';

导入环回的默认值 UserServiceBindings,这会导致问题。通过用新的导入替换导入到我的 UserServiceBindings:

import {UserServiceBindings} from './mybindings';

已解决问题。