import * as bcrypt from 'bcrypt' ; import { Injectable } from '@nestjs/common' ; import { UserRepository } from '@notifire/dal' ; import { LoginCommand } from './login.command' ; import { ApiException } from '../../../shared/exceptions/api.exception' ; import { normalizeEmail } from '../../../shared/helpers/email-normalization.service' ; import { AuthService } from '../../services/auth.service' ; @ Injectable ( ) export class Login { constructor ( private userRepository , private authService ) { } async execute ( command ) { const email = normalizeEmail ( command . email ) ; const user = await this . userRepository . findByEmail ( email ) ; if ( ! user ) throw new ApiException ( 'User not found' ) ; if ( ! user . password ) throw new ApiException ( 'OAuth user login attempt' ) ; const isMatching = await bcrypt . compare ( command . password , user . password ) ; if ( ! isMatching ) throw new ApiException ( 'Wrong credentials provided' ) ; return { token : await this . authService . generateUserToken ( user ) , } ; } }