如何解决Loopback 4 将 USER_SERVICE 与自定义用户服务绑定
我正在尝试使用实现 MyUserService
的自定义 UserService<MyUser,MyCredentials>
:
export class MyUserService implements UserService<Account,LoginCredential> { ... }
与环回的User
和Credentials
的区别在于:
-
MyUser.id
是一个number
(而环回的User.id
是一个string
) -
MyCredentials
有属性:identity
和secret
(而环回的Credentials
有属性:email
和secret
) -
MyUser.credential
是关系(而不是环回的User.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,除非我遵循以下规则:
- 用户凭据的属性必须正好是
userCredentials
- 凭据必须具有
email
和password
属性 - 用户 ID 必须是
string
类型 - ...等(可能错过了更多的限制)
我的问题是:
如何绑定我的自定义用户服务,在那里我可以自定义存储在 credentials
中的数据?
解决方法
问题比我想象的要简单。长话短说:
只需确保我导入的是我自己的 UserServiceBindings 而不是 Loopback 的默认 UserServiceBindings
。
就我而言,UserServiceBindings
中的 application.ts
如下:
import {UserServiceBindings} from '@loopback/authentication-jwt';
导入 Loopback 的默认 UserServiceBindings
,这会导致问题。通过用新的导入替换导入到我的 UserServiceBindings
:
import {UserServiceBindings} from './mybindings';
已解决问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。