Ошибка разрешения 500: ключ «services.authentication.basic.user.service» не привязан ни к какому значению в контексте

#node.js #typescript #loopback4

#node.js #typescript #loopback4

Вопрос:

Я пытаюсь создать пользовательскую аутентификацию с помощью обратной связи 4, у меня есть ссылка: https://loopback.io/doc/en/lb4/Implement-your-own-strategy.html services.authentication.basic.user.service не привязан.

 > user.controller.ts
  

Это user.controller, в который я ввел JWTAuthenticationStrategyBindings, BasicAuthenticationStrategyBindings

    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;
  }
}
  

Я новичок в loopback 4, я хочу создать пользовательскую аутентификацию для моего нового проекта. итак, я пытаюсь создать фиктивный проект для моего следующего проекта.

Ответ №1:

Я исправил эту ошибку, нам просто нужно добавить для привязки в контексте

application.ts

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

примечание: приведенный выше код добавляется в конструктор