绑定自定义服务时出现环回4身份验证错误 [英] Loopback 4 authentication error while binding custom service

查看:97
本文介绍了绑定自定义服务时出现环回4身份验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循此链接在回送4中创建用于身份验证的自定义服务.我成功创建了服务,但是在application.ts中绑定此服务时,出现以下错误.

I have followed this link for creating custom service for authentication in loopback 4. I successfully created the service but while binding this service in application.ts I am getting the following error.

"typeof CustomUserService"类型的参数不可分配给 类型为'Constructor>'的参数.
构造签名返回类型'CustomUserService'和 "UserService"不兼容. "verifyCredentials(...)"返回的类型在这些类型之间不兼容. 输入承诺" 不可分配给类型 '承诺'. 类型'import("d:/ionic-pr/loopback-projects/custom/src/models/user.model")中缺少属性'userCredentials'.用户' 但必须输入 'import("d:/ionic-pr/loopback-projects/custom/node_modules/@loopback/authentication-jwt/dist/models/user.model").用户"

Argument of type 'typeof CustomUserService' is not assignable to parameter of type 'Constructor>'.
Construct signature return types 'CustomUserService' and 'UserService' are incompatible. The types returned by 'verifyCredentials(...)' are incompatible between these types. Type 'Promise' is not assignable to type 'Promise'. Property 'userCredentials' is missing in type 'import("d:/ionic-pr/loopback-projects/custom/src/models/user.model").User' but required in type 'import("d:/ionic-pr/loopback-projects/custom/node_modules/@loopback/authentication-jwt/dist/models/user.model").User'

GIT存储库- https://github.com/pratikjaiswal15/loopbck4uth

我有两个模型user和userCred具有一个分配权.关系名称为userCred.

I have two models user and userCred with has one realtionship. The relation name is userCred.

这是代码

提前谢谢

application.ts

application.ts

import {UserCredRepository, UserRepository} from './repositories';
import {CustomUserService} from './services/custom-user.service';
import {UserServiceBindings} from '@loopback/authentication-jwt';

// Bind user service inside constructor
    this.bind(UserServiceBindings.USER_SERVICE).toClass(CustomUserService),// error on this line

      // Bind user and credentials repository
      this.bind(UserServiceBindings.USER_REPOSITORY).toClass(
        UserRepository,
      ),

      this.bind(UserServiceBindings.USER_CREDENTIALS_REPOSITORY).toClass(
        UserCredRepository,
      )

custom-service.ts

custom-service.ts

import {UserService} from '@loopback/authentication';
import {repository} from '@loopback/repository';
import {HttpErrors} from '@loopback/rest';
import {securityId, UserProfile} from '@loopback/security';
import {compare} from 'bcryptjs';
// User --> MyUser
import {User} from '../models/user.model';
// UserRepository --> MyUserRepository
import {Credentials, UserRepository} from '../repositories/user.repository';


export class CustomUserService implements UserService<User, Credentials> {
  constructor(
    // UserRepository --> MyUserRepository
    @repository(UserRepository) public userRepository: UserRepository,
  ) {}

  // User --> MyUser
  async verifyCredentials(credentials: Credentials): Promise<User> {
    const invalidCredentialsError = 'Invalid email or password.';

    const foundUser = await this.userRepository.findOne({
      where: {email: credentials.email},
    });
    if (!foundUser) {
      throw new HttpErrors.Unauthorized(invalidCredentialsError);
    }

    const credentialsFound = await this.userRepository.findCredentials(
      foundUser.id,
    );
    if (!credentialsFound) {
      throw new HttpErrors.Unauthorized(invalidCredentialsError);
    }

    const passwordMatched = await compare(
      credentials.password,
      credentialsFound.password,
    );

    if (!passwordMatched) {
      throw new HttpErrors.Unauthorized(invalidCredentialsError);
    }

    return foundUser;
  }

  // User --> MyUser
  convertToUserProfile(user: User): UserProfile {

    let address = ''
    if (user.address) {
      address = user.address
    }

    return {
      [securityId]: user.id!.toString(),
      name: user.name,
      id: user.id,
      email: user.email,
      address: address

    };


  }
}

user.repositor.ts

user.repositor.ts

import {Getter, inject} from '@loopback/core';
import {DefaultCrudRepository, HasOneRepositoryFactory, repository} from '@loopback/repository';
import {MygodDataSource} from '../datasources';
import {User, UserCred, UserRelations} from '../models';
import {UserCredRepository} from './user-cred.repository';

export type Credentials = {
  email: string;
  password: string;
};



export class UserRepository extends DefaultCrudRepository<
  User,
  typeof User.prototype.id,
  UserRelations
  > {

  public readonly userCred: HasOneRepositoryFactory<UserCred, typeof User.prototype.id>;

  constructor(
    @inject('datasources.mygod') dataSource: MygodDataSource, @repository.getter('UserCredRepository') protected userCredRepositoryGetter: Getter<UserCredRepository>,
  ) {
    super(User, dataSource);
    this.userCred = this.createHasOneRepositoryFactoryFor('userCred', userCredRepositoryGetter);
    this.registerInclusionResolver('userCred', this.userCred.inclusionResolver);
  }


  async findCredentials(
    userId: typeof User.prototype.id,
  ): Promise<UserCred | undefined> {
    try {
      return await this.userCred(userId).get();
    } catch (err) {
      if (err.code === 'ENTITY_NOT_FOUND') {
        return undefined;
      }
      throw err;
    }
  }
}

推荐答案

在您的git帖子中对该问题进行了简要说明 https://github.com/strongloop/loopback-next/issues/5541

have a brief explaination of the issue at your git post Follow you answer at https://github.com/strongloop/loopback-next/issues/5541

这篇关于绑定自定义服务时出现环回4身份验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆