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> { ... }
与环回的 User
和 Credentials
的区别是:
MyUser.id
是 number
(而环回的 User.id
是 string
)
MyCredentials
具有属性:identity
和 secret
(而环回的 Credentials
具有属性:email
和 secret
)
MyUser.credential
是关系(而不是loopback的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
.
就我而言,我在 application.ts
中的 UserServiceBindings
如下:
import {UserServiceBindings} from '@loopback/authentication-jwt';
导入环回的默认值 UserServiceBindings
,这会导致问题。通过用新的导入替换导入到我的 UserServiceBindings
:
import {UserServiceBindings} from './mybindings';
已解决问题。
我正在尝试使用实现 UserService<MyUser, MyCredentials>
:
MyUserService
export class MyUserService implements UserService<Account, LoginCredential> { ... }
与环回的 User
和 Credentials
的区别是:
MyUser.id
是number
(而环回的User.id
是string
)MyCredentials
具有属性:identity
和secret
(而环回的Credentials
具有属性:email
和secret
)MyUser.credential
是关系(而不是loopback的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
.
就我而言,我在 application.ts
中的 UserServiceBindings
如下:
import {UserServiceBindings} from '@loopback/authentication-jwt';
导入环回的默认值 UserServiceBindings
,这会导致问题。通过用新的导入替换导入到我的 UserServiceBindings
:
import {UserServiceBindings} from './mybindings';
已解决问题。