验证后环回 4 错误。键 'authentication.currentUser' 未绑定到上下文应用程序中的任何值
Loopback 4 error after authentication. The key 'authentication.currentUser' is not bound to any value in context application
我一直在使用 Loopback 4。我最近遇到了一个错误。身份验证成功后,我尝试从我的端点访问当前用户。我得到这个错误
13:31:07 0|index | Unhandled error in POST /posts: 500 Error: The key 'authentication.currentUser' is not bound to any value in context application
13:31:07 0|index | at HomeApplication.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:596:15)
13:31:07 0|index | at RestServer.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:31:07 0|index | at RequestContext.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:31:07 0|index | at RequestContext.getValueOrPromise (/Development/home/node_modules/@loopback/context/dist/context.js:651:30)
13:31:07 0|index | at RequestContext.get (/Development/home/node_modules/@loopback/context/dist/context.js:574:21)
13:31:07 0|index | at PostController.getter [as getCurrentUser] (/Development/home/node_modules/@loopback/context/dist/inject.js:258:20)
13:31:07 0|index | at PostController.<anonymous> (/Development/home/dist/controllers/user/post.controller.js:57:35)
我已经在 GitHub 和 Whosebug 上尝试了针对此问题的所有可用解决方案。这几天一直在寻找解决方案。
我还尝试通过将控制台日志放入生成的文件中来自行调试。这就是我在 auth-action.provider.js
中所做的
async action(request) {
const strategy = await this.getStrategy();
if (!strategy) {
// The invoked operation does not require authentication.
return undefined;
}
console.log(`Strategy obtained successfully`)
const userProfile = await strategy.authenticate(request);
if (!userProfile) {
// important to throw a non-protocol-specific error here
const error = new Error(`User profile not returned from strategy's authenticate function`);
Object.assign(error, {
code: types_1.USER_PROFILE_NOT_FOUND,
});
throw error;
}
console.log(`User obtained successfully`)
console.log(JSON.stringify(userProfile))
this.setCurrentUser(userProfile);
console.log(`Called setCurrentUser`)
return userProfile;
}
这之后的结果
13:46:55 0|index | Authenticating
13:46:55 0|index | Strategy obtained successfully
13:46:55 0|index | ~~ Authenticating: token --> 9823eb1940eae6df49c54698d8a71b319f0b00635321a02632965a7667d69ce68883b61d803f0691c2b393bc9841606b153fa28fc853ecfd41bd647725479b54, mode --> personal~~
13:46:55 0|index | User found. Id --> 5d8f549de7179a022443e34e
13:46:55 0|index | User obtained successfully
13:46:55 0|index | {"email":"my@email.com","id":"5d8f549de7179a022443e34e","name":"name","image":null,"designation":"Designation","company":"string"}
13:46:55 0|index | Called setCurrentUser
13:46:55 0|index | Trace: Error: The key 'authentication.currentUser' is not bound to any value in context application
13:46:55 0|index | at HomeApplication.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:596:15)
13:46:55 0|index | at RestServer.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:46:55 0|index | at RequestContext.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:46:55 0|index | at RequestContext.getValueOrPromise (/Development/home/node_modules/@loopback/context/dist/context.js:651:30)
13:46:55 0|index | at RequestContext.get (/Development/home/node_modules/@loopback/context/dist/context.js:574:21)
13:46:55 0|index | at PostController.getter [as getCurrentUser] (/Development/home/node_modules/@loopback/context/dist/inject.js:258:20)
13:46:55 0|index | at PostController.<anonymous> (/Development/home/dist/controllers/user/post.controller.js:57:35)
如您所见,一切都在按预期进行。只是当我尝试让用户进入我的控制器时,会发生此错误。如果我不尝试访问用户,一切正常。
我的猜测是在 auth-action.provider.
中注入的 setCurrentUser 方法有问题
如果你需要的话,这是我的控制器。
export class PostController {
static MAX_POST_LENGTH = 800;
constructor(
@repository(PostRepository)
public postRepository: PostRepository,
@inject(MyAuthBindings.USER_TOKEN_SERVICE)
public userTokenService: UserTokenService,
@inject.getter(AuthenticationBindings.CURRENT_USER)
public getCurrentUser: Getter<MyUserProfile>,
) {
}
@post('/posts', {
responses: {
'200': {
description: 'Post model instance',
content: {'application/json': {schema: {'x-ts-type': UserPost}}},
},
},
})
@authenticate('user')
async create(
@param.header.string('token') token: string,
@param.header.string('mode') mode: string,
@requestBody() newPostRequest: PostRequest): Promise<UserPost> {
if (newPostRequest.body.length > PostController.MAX_POST_LENGTH) {
throw new HttpErrors.BadRequest(`Post body cannot be larger than ${PostController.MAX_POST_LENGTH} characters`);
}
let user = await this.getCurrentUser();
console.log(`Authenticated user --> ${JSON.stringify(user, null, 2)}`);
let postItem = new Post(newPostRequest);
postItem.userId = user.id;
postItem.createdAt = new Date();
postItem.updatedAt = new Date();
postItem = await this.postRepository.create(postItem);
if (postItem.id != null)
PostController.checkAndSendStyleworkPostNotification(user.id, postItem.id);
return new UserPost(postItem, user, user);
}
上述方法处理不到日志
P.S。 @loopback/authentication 从 2.2.2 更新到 3.0.0
后开始出现这个问题
我解决了这个问题。正如 Authentication component documentation 中所指出的那样,该问题 AuthenticationBindings.CURRENT_USER 现在是
SecurityBindings.USER 在@loopback/security.
我只需要将 AuthenticationBindings.CURRENT_USER 替换为 SecurityBindings.USER。正如我在问题中指出的那样,此更改是在 v3.0.0 中进行的。
我一直在使用 Loopback 4。我最近遇到了一个错误。身份验证成功后,我尝试从我的端点访问当前用户。我得到这个错误
13:31:07 0|index | Unhandled error in POST /posts: 500 Error: The key 'authentication.currentUser' is not bound to any value in context application
13:31:07 0|index | at HomeApplication.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:596:15)
13:31:07 0|index | at RestServer.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:31:07 0|index | at RequestContext.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:31:07 0|index | at RequestContext.getValueOrPromise (/Development/home/node_modules/@loopback/context/dist/context.js:651:30)
13:31:07 0|index | at RequestContext.get (/Development/home/node_modules/@loopback/context/dist/context.js:574:21)
13:31:07 0|index | at PostController.getter [as getCurrentUser] (/Development/home/node_modules/@loopback/context/dist/inject.js:258:20)
13:31:07 0|index | at PostController.<anonymous> (/Development/home/dist/controllers/user/post.controller.js:57:35)
我已经在 GitHub 和 Whosebug 上尝试了针对此问题的所有可用解决方案。这几天一直在寻找解决方案。
我还尝试通过将控制台日志放入生成的文件中来自行调试。这就是我在 auth-action.provider.js
中所做的async action(request) {
const strategy = await this.getStrategy();
if (!strategy) {
// The invoked operation does not require authentication.
return undefined;
}
console.log(`Strategy obtained successfully`)
const userProfile = await strategy.authenticate(request);
if (!userProfile) {
// important to throw a non-protocol-specific error here
const error = new Error(`User profile not returned from strategy's authenticate function`);
Object.assign(error, {
code: types_1.USER_PROFILE_NOT_FOUND,
});
throw error;
}
console.log(`User obtained successfully`)
console.log(JSON.stringify(userProfile))
this.setCurrentUser(userProfile);
console.log(`Called setCurrentUser`)
return userProfile;
}
这之后的结果
13:46:55 0|index | Authenticating
13:46:55 0|index | Strategy obtained successfully
13:46:55 0|index | ~~ Authenticating: token --> 9823eb1940eae6df49c54698d8a71b319f0b00635321a02632965a7667d69ce68883b61d803f0691c2b393bc9841606b153fa28fc853ecfd41bd647725479b54, mode --> personal~~
13:46:55 0|index | User found. Id --> 5d8f549de7179a022443e34e
13:46:55 0|index | User obtained successfully
13:46:55 0|index | {"email":"my@email.com","id":"5d8f549de7179a022443e34e","name":"name","image":null,"designation":"Designation","company":"string"}
13:46:55 0|index | Called setCurrentUser
13:46:55 0|index | Trace: Error: The key 'authentication.currentUser' is not bound to any value in context application
13:46:55 0|index | at HomeApplication.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:596:15)
13:46:55 0|index | at RestServer.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:46:55 0|index | at RequestContext.getBinding (/Development/home/node_modules/@loopback/context/dist/context.js:592:33)
13:46:55 0|index | at RequestContext.getValueOrPromise (/Development/home/node_modules/@loopback/context/dist/context.js:651:30)
13:46:55 0|index | at RequestContext.get (/Development/home/node_modules/@loopback/context/dist/context.js:574:21)
13:46:55 0|index | at PostController.getter [as getCurrentUser] (/Development/home/node_modules/@loopback/context/dist/inject.js:258:20)
13:46:55 0|index | at PostController.<anonymous> (/Development/home/dist/controllers/user/post.controller.js:57:35)
如您所见,一切都在按预期进行。只是当我尝试让用户进入我的控制器时,会发生此错误。如果我不尝试访问用户,一切正常。
我的猜测是在 auth-action.provider.
中注入的 setCurrentUser 方法有问题如果你需要的话,这是我的控制器。
export class PostController {
static MAX_POST_LENGTH = 800;
constructor(
@repository(PostRepository)
public postRepository: PostRepository,
@inject(MyAuthBindings.USER_TOKEN_SERVICE)
public userTokenService: UserTokenService,
@inject.getter(AuthenticationBindings.CURRENT_USER)
public getCurrentUser: Getter<MyUserProfile>,
) {
}
@post('/posts', {
responses: {
'200': {
description: 'Post model instance',
content: {'application/json': {schema: {'x-ts-type': UserPost}}},
},
},
})
@authenticate('user')
async create(
@param.header.string('token') token: string,
@param.header.string('mode') mode: string,
@requestBody() newPostRequest: PostRequest): Promise<UserPost> {
if (newPostRequest.body.length > PostController.MAX_POST_LENGTH) {
throw new HttpErrors.BadRequest(`Post body cannot be larger than ${PostController.MAX_POST_LENGTH} characters`);
}
let user = await this.getCurrentUser();
console.log(`Authenticated user --> ${JSON.stringify(user, null, 2)}`);
let postItem = new Post(newPostRequest);
postItem.userId = user.id;
postItem.createdAt = new Date();
postItem.updatedAt = new Date();
postItem = await this.postRepository.create(postItem);
if (postItem.id != null)
PostController.checkAndSendStyleworkPostNotification(user.id, postItem.id);
return new UserPost(postItem, user, user);
}
上述方法处理不到日志
P.S。 @loopback/authentication 从 2.2.2 更新到 3.0.0
后开始出现这个问题我解决了这个问题。正如 Authentication component documentation 中所指出的那样,该问题 AuthenticationBindings.CURRENT_USER 现在是 SecurityBindings.USER 在@loopback/security.
我只需要将 AuthenticationBindings.CURRENT_USER 替换为 SecurityBindings.USER。正如我在问题中指出的那样,此更改是在 v3.0.0 中进行的。