Search code examples
node.jstypescriptloopback4

500 ResolutionError: The key 'services.authentication.basic.user.service' is not bound to any value in context


I have trying to create custom authentication using loopback 4 I have ref: https://loopback.io/doc/en/lb4/Implement-your-own-strategy.html services.authentication.basic.user.service is not bound.

This is user.controller I have inject 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;
  }
}

I'm new in loopback 4 I wanna to create custom authentication for my new project. so i'm try to creating dummy project for my next project.


Solution

  • I have resoled this error we just need add to bind in context

    application.ts

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

    note: above code add in constructer