File

apps/api/src/user/user.service.ts

Index

Methods

Constructor

constructor(userRepository: Repository<UserEntity>, configService: ConfigService)
Parameters :
Name Type Optional
userRepository Repository<UserEntity> No
configService ConfigService No

Methods

Async create
create(data: Partial<UserEntity>)
Parameters :
Name Type Optional
data Partial<UserEntity> No
Async findOne
findOne(where: FindOneOptions<UserEntity>)
Parameters :
Name Type Optional
where FindOneOptions<UserEntity> No
Async setNewPassword
setNewPassword(id: number, password: string)
Parameters :
Name Type Optional
id number No
password string No
Async setPasswordResetCode
setPasswordResetCode(id: number, passwordResetCode: string)
Parameters :
Name Type Optional
id number No
passwordResetCode string No
Async setTwoFactorAuthenticationSecret
setTwoFactorAuthenticationSecret(user: UserEntity, secret: string)
Parameters :
Name Type Optional
user UserEntity No
secret string No
Async setupTwoFactorAuthentication
setupTwoFactorAuthentication(user: UserEntity, enable)
Parameters :
Name Type Optional Default value
user UserEntity No
enable No false
Async storeRefreshToken
storeRefreshToken(user: UserEntity, token: string)
Parameters :
Name Type Optional
user UserEntity No
token string No
Async turnOfTwoFactorAuthentication
turnOfTwoFactorAuthentication(user: UserEntity)
Parameters :
Name Type Optional
user UserEntity No
Async update
update(id: number, updates: UserUpdate)
Parameters :
Name Type Optional
id number No
updates UserUpdate No
import { Injectable, NotFoundException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository, FindOneOptions } from 'typeorm'

import { UserUpdate } from './dto/user-update.dto'
import { UserEntity } from '../entities/user.entity'
import { ConfigService } from '@nestjs/config'
import { DateTime } from 'luxon'

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(UserEntity)
    private readonly userRepository: Repository<UserEntity>,
    private readonly configService: ConfigService
  ) {}

  async create(data: Partial<UserEntity>): Promise<UserEntity> {
    const user = this.userRepository.create({ ...data, isTwoFAEnabled: false })

    return this.userRepository.save(user)
  }

  async findOne(where: FindOneOptions<UserEntity>): Promise<UserEntity> {
    const user = await this.userRepository.findOne(where)

    if (!user) {
      throw new NotFoundException(
        `There isn't any user with identifier: ${where}`
      )
    }

    return user
  }

  async update(id: number, updates: UserUpdate): Promise<UserEntity> {
    const user = await this.userRepository.findOneBy({ id })

    if (!user) {
      throw new NotFoundException(`There isn't any user with id: ${id}`)
    }

    this.userRepository.merge(user, updates)

    return this.userRepository.save(user)
  }

  async setPasswordResetCode(
    id: number,
    passwordResetCode: string
  ): Promise<UserEntity> {
    const user = await this.userRepository.findOneBy({ id })

    if (!user) {
      throw new NotFoundException(`There isn't any user with id: ${id}`)
    }

    const resetPasswordPeriod = this.configService.get<number>(
      'RESET_PASSWORD_PERIOD'
    )
    const expiredTime = DateTime.now()
      .plus({ minute: resetPasswordPeriod })
      .toFormat('yyyy-MM-dd HH:mm:ss')

    user.passwordResetCode = passwordResetCode
    user.passwordResetExpiredTime = expiredTime
    return this.userRepository.save(user)
  }

  async setNewPassword(id: number, password: string): Promise<UserEntity> {
    const user = await this.userRepository.findOneBy({ id })

    if (!user) {
      throw new NotFoundException(`There isn't any user with id: ${id}`)
    }

    user.password = password
    user.passwordResetCode = null
    user.passwordResetExpiredTime = null

    return this.userRepository.save(user)
  }

  async storeRefreshToken(
    user: UserEntity,
    token: string
  ): Promise<UserEntity> {
    user.refreshToken = token
    return this.userRepository.save(user)
  }

  async setTwoFactorAuthenticationSecret(
    user: UserEntity,
    secret: string
  ): Promise<UserEntity> {
    user.twoFASecret = secret
    return this.userRepository.save(user)
  }

  async setupTwoFactorAuthentication(
    user: UserEntity,
    enable = false
  ): Promise<UserEntity> {
    user.isTwoFAEnabled = enable
    return this.userRepository.save(user)
  }

  async turnOfTwoFactorAuthentication(user: UserEntity): Promise<UserEntity> {
    user.isTwoFAEnabled = false
    user.twoFASecret = ''
    return this.userRepository.save(user)
  }
}

results matching ""

    No results matching ""