All files / src/services user-service.ts

95.83% Statements 23/24
75% Branches 3/4
100% Functions 3/3
95.45% Lines 21/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52  1x 1x 1x 1x   1x   1x 1x 1x 1x 1x     93x       93x 93x           5x   5x         5x     5x 5x 1x 4x       7x                
import {UserService} from '@loopback/authentication';
import {inject} from '@loopback/core';
import {repository} from '@loopback/repository';
import {Response, RestBindings} from '@loopback/rest';
import {securityId, UserProfile} from '@loopback/security';
import {Credentials} from '../controllers/types';
import {PasswordHasherBindings} from '../keys';
import {User} from '../models';
import {UserRepository} from '../repositories/user.repository';
import errorMessages from '../utils/errorMessages';
import {BcryptHasher} from './hash.password';
const {invalidUserCredentials, userNotFound} = errorMessages
export class MyUserService implements UserService<User, Credentials>{
  constructor(
    @repository(UserRepository)
    public userRepository: UserRepository,
 
    // @inject('service.hasher')
    @inject(PasswordHasherBindings.PASSWORD_HASHER)
    public hasher: BcryptHasher,
    @inject(RestBindings.Http.RESPONSE) protected res: Response,
    // @inject(ControllerKey.AUTH_CONTROLLER) public authController: AuthController,
 
 
  ) { }
  async verifyCredentials(credentials: Credentials): Promise<User> {
    let methodName = "verifyCredentials"
    // implement this method
    const foundUser = await this.userRepository.findOne({
      where: {
        email: credentials.email
      }
    });
    Iif (!foundUser) {
      throw {code: 422, message: userNotFound, methodName}
    }
    const passwordMatched = await this.hasher.comparePassword(credentials.password, foundUser.password)
    if (!passwordMatched)
      throw {code: 422, message: invalidUserCredentials, methodName}
    return foundUser;
  }
 
  convertToUserProfile(user: User): UserProfile {
    return {
      [securityId]: user.UID,
      UID: user.UID,
    };
    // throw new Error('Method not implemented.');
  }
 
}