import { Injectable , HttpException , HttpStatus , Logger } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { JwtService } from '@nestjs/jwt' ; import { UserLoginDTO } from '../service/dto/user-login.dto' ; import { Payload } from '../security/payload.interface' ; import * as bcrypt from 'bcrypt' ; import { AuthorityRepository } from '../repository/authority.repository' ; import { UserService } from '../service/user.service' ; import { UserDTO } from './dto/user.dto' ; import { FindManyOptions } from 'typeorm' ; @ Injectable ( ) export class AuthService { logger = new Logger ( 'AuthService' ) ; constructor ( private readonly jwtService , @ InjectRepository ( AuthorityRepository ) private authorityRepository , private userService ) { } async login ( userLogin ) { const loginUserName = userLogin . username ; const loginPassword = userLogin . password ; const userFind = await this . userService . findByFields ( { where : { login : loginUserName } } ) ; const validPassword = ! ! userFind && ( await bcrypt . compare ( loginPassword , userFind . password ) ) ; if ( ! userFind || ! validPassword ) { throw new HttpException ( 'Invalid login name or password!' , HttpStatus . BAD_REQUEST ) ; } if ( userFind && ! userFind . activated ) { throw new HttpException ( 'Your account is not been activated!' , HttpStatus . BAD_REQUEST ) ; } const user = await this . findUserWithAuthById ( userFind . id ) ; const payload = { id : user . id , username : user . login , authorities : user . authorities } ; return { id_token : this . jwtService . sign ( payload ) , } ; } async validateUser ( payload ) { return await this . findUserWithAuthById ( payload . id ) ; } async findUserWithAuthById ( userId ) { const userDTO = await this . userService . findByFields ( { where : { id : userId } } ) ; return userDTO ; } async getAccount ( userId ) { const userDTO = await this . findUserWithAuthById ( userId ) ; if ( ! userDTO ) { return ; } return userDTO ; } async changePassword ( userLogin , currentClearTextPassword , newPassword ) { const userFind = await this . userService . findByFields ( { where : { login : userLogin } } ) ; if ( ! userFind ) { throw new HttpException ( 'Invalid login name!' , HttpStatus . BAD_REQUEST ) ; } if ( ! ( await bcrypt . compare ( currentClearTextPassword , userFind . password ) ) ) { throw new HttpException ( 'Invalid password!' , HttpStatus . BAD_REQUEST ) ; } userFind . password = newPassword ; await this . userService . save ( userFind , userLogin , true ) ; return ; } async registerNewUser ( newUser ) { let userFind = await this . userService . findByFields ( { where : { login : newUser . login } } ) ; if ( userFind ) { throw new HttpException ( 'Login name already used!' , HttpStatus . BAD_REQUEST ) ; } userFind = await this . userService . findByFields ( { where : { email : newUser . email } } ) ; if ( userFind ) { throw new HttpException ( 'Email is already in use!' , HttpStatus . BAD_REQUEST ) ; } newUser . authorities = [ 'ROLE_USER' ] ; const user = await this . userService . save ( newUser , newUser . login , true ) ; return user ; } async updateUserSettings ( userLogin , newUserInfo ) { const userFind = await this . userService . findByFields ( { where : { login : userLogin } } ) ; if ( ! userFind ) { throw new HttpException ( 'Invalid login name!' , HttpStatus . BAD_REQUEST ) ; } const userFindEmail = await this . userService . findByFields ( { where : { email : newUserInfo . email } } ) ; if ( userFindEmail && newUserInfo . email !== userFind . email ) { throw new HttpException ( 'Email is already in use!' , HttpStatus . BAD_REQUEST ) ; } userFind . firstName = newUserInfo . firstName ; userFind . lastName = newUserInfo . lastName ; userFind . email = newUserInfo . email ; userFind . langKey = newUserInfo . langKey ; await this . userService . save ( userFind , userLogin ) ; return ; } async getAllUsers ( options ) { return await this . userService . findAndCount ( options ) ; } }