All files / app/auth authentication-service.ts

0% Statements 0/23
0% Branches 0/4
0% Functions 0/1
0% Lines 0/23

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                                                                                                                             
import {User, UserDto, UserRepository, UserService} from '@models';
import {UserNotFoundError} from '@errors';
import {debug} from 'debug';
import bindToLog from '@util/user-bound-logging';
import {ServiceContext} from '@models/service';
import ModelToDtoConverter from '@util/model-to-dto-converter';
 
const log = debug('group-car:auth');
const error = debug('group-car:auth:error');
 
/**
 * Authentication service.
 */
export class AuthenticationService {
  /**
   * Tries to log in the user with the given username and password.
   *
   * First, check if a user with the given username exists.
   * Then, check if the given password matches the password of the user.
   * Lastly, return the dto version of the user model.
   *
   * If any step fails, immediately return null.
   * @param username - Username
   * @param password - Password
   * @param ip - IP of the request (from context)
   */
  public static async login(
      username: string,
      password: string,
      {ip}: ServiceContext,
  ): Promise<UserDto | null> {
    const ipLog = bindToLog(log, {prefix: 'IP %s: ', args: [ip]});
    const ipError = bindToLog(error, {prefix: 'IP %s: ', args: [ip]});
 
    // Check if user exists
    let user: User;
    try {
      ipLog('Checking if user with username exists');
      user = await UserRepository.findByUsername(username);
    } catch (e) {
      if (e instanceof UserNotFoundError) {
        ipError('User with username "%s" doesn\'t exist', username);
        return null;
      } else {
        ipError(
            'Unexpected error occurred while searching ' +
            'for user with username "%s": %s',
            username,
            e,
        );
        // Rethrow
        throw e;
      }
    }
 
    // Validate password
    const passwordIsCorrect = await UserService.checkPassword(
        user.password,
        password,
    );
 
    if (passwordIsCorrect) {
      return ModelToDtoConverter.convertSequelizeModel(user, UserDto);
    } else {
      return null;
    }
  }
}
 
export default AuthenticationService;