NodeJS 500 ResolutionError:键“services.authentication.basic.user.service”未绑定到上下文中的任何值

kmynzznz  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(81)

我尝试使用EQUIP4创建自定义身份验证我有参考:https://loopback.io/doc/en/lb4/Implement-your-own-strategy.html services.authentication.basic.user.service未绑定
这是user.controller,我注入了JWTAuthenticationStrategyBindingsBasicAuthenticationStrategyBindings

import {
  authenticate,
  TokenService,
  AuthenticationBindings,
} from '@loopback/authentication';

import {inject, Getter} from '@loopback/core';
import {model, property, repository} from '@loopback/repository';
import {
  get,
  getModelSchemaRef,
  post,
  requestBody,
  SchemaObject,
} from '@loopback/rest';
import {SecurityBindings, securityId, UserProfile} from '@loopback/security';
import {genSalt, hash} from 'bcryptjs';
import _ from 'lodash';
import {User} from '../models';
import {UserRepository} from '../customStrategy/__tests__/fixtures/users/user.repository';
import {BasicAuthenticationUserService} from '../customStrategy/__tests__/fixtures/services/basic-auth-user-service';
import {
  BasicAuthenticationStrategyBindings,
  JWTAuthenticationStrategyBindings,
} from '../customStrategy/__tests__/fixtures/keys';

export class UserController {
  constructor(
    @inject(JWTAuthenticationStrategyBindings.TOKEN_SERVICE)
    public jwtService: TokenService,
    @inject(BasicAuthenticationStrategyBindings.USER_SERVICE)
    public userService: BasicAuthenticationUserService,
    @inject(SecurityBindings.USER, {optional: true})
    public user: UserProfile,
    @repository(UserRepository) protected userRepository: UserRepository,
  ) {}

  @get('/auth/me', {
    responses: {
      '200': {
        description: 'The current user profile',
        content: {
          'application/json': {
            schema: CredentialsSchema,
          },
        },
      },
    },
  })
  @authenticate('basic')
  async printCurrentUser(
    @inject('authentication.currentUser') user: UserProfile,
  ): Promise<UserProfile> {
    return user;
  }
}

我是2014年的新手,我想为我的新项目创建自定义身份验证。所以我尝试为我下一个项目创建虚拟项目。

bnlyeluc

bnlyeluc1#

我已经解决了这个错误我们只需要在上下文中添加绑定

application.ts

this.bind('services.authentication.basic.user.service.binding').toClass(
  BasicAuthenticationUserService,
);

注意:以上代码添加到构造函数中

相关问题