环回 4:如何在终结点中注入用户



我有一个端点,用户和来宾(未经身份验证(都可以将数据发布到:

async create(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
create_account: {type: 'boolean'},
password: {type: 'string'},
password_repeat: {type: 'string'},
currency: {type: 'string'},
payment_method: {type: 'string'},
products: {type: 'array'},
voucher: {type: 'string'},
customer: {type: 'object'},
news_letter: {type: 'boolean'},
},
},
},
},
})
@inject(SecurityBindings.USER) currentUserProfile: UserProfile,
order: Omit<Order, 'id'>,
): Promise<{url: string}> {
const userId = currentUserProfile[securityId];
}

但是,我不确定如何从会话中获取登录用户,因为我收到以下错误:

The key 'security.user' is not bound to any value in context

在这种情况下,如何获取用户 ID?

控制器端点需要使用@authenticate()@authorize()装饰器进行修饰,并且必须事先设置身份验证系统。

身份验证和授权文档最近进行了全面修订。请参考它们作为权威指南。

例如

@post('/users/{userId}/orders', {
responses: {
'200': {
description: 'User.Order model instance',
content: {'application/json': {schema: {'x-ts-type': Order}}},
},
},
})
@authenticate('jwt')
@authorize({resource: 'order', scopes: ['create']})
async createOrder(
@param.path.string('userId') userId: string,
@requestBody() order: Order,
): Promise<Order> {
await this.userRepo.orders(userId).create(order);
}

不幸的是,如果没有更多信息(例如身份验证策略和授权提供程序(,就不可能给出明确的解决方案,因为不同的UAA实现将具有不同的解决方案。

延伸阅读

  • https://loopback.io/doc/en/lb4/Authentication-overview.html
  • https://loopback.io/doc/en/lb4/Loopback-component-authorization.html

最新更新