apps/api/src/user/user.service.ts
Methods |
|
constructor(userRepository: Repository<UserEntity>, configService: ConfigService)
|
|||||||||
Defined in apps/api/src/user/user.service.ts:11
|
|||||||||
Parameters :
|
Async create | ||||||
create(data: Partial<UserEntity>)
|
||||||
Defined in apps/api/src/user/user.service.ts:18
|
||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async findOne | ||||||
findOne(where: FindOneOptions<UserEntity>)
|
||||||
Defined in apps/api/src/user/user.service.ts:24
|
||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async setNewPassword |
setNewPassword(id: number, password: string)
|
Defined in apps/api/src/user/user.service.ts:70
|
Returns :
Promise<UserEntity>
|
Async setPasswordResetCode |
setPasswordResetCode(id: number, passwordResetCode: string)
|
Defined in apps/api/src/user/user.service.ts:48
|
Returns :
Promise<UserEntity>
|
Async setTwoFactorAuthenticationSecret | |||||||||
setTwoFactorAuthenticationSecret(user: UserEntity, secret: string)
|
|||||||||
Defined in apps/api/src/user/user.service.ts:92
|
|||||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async setupTwoFactorAuthentication | ||||||||||||
setupTwoFactorAuthentication(user: UserEntity, enable)
|
||||||||||||
Defined in apps/api/src/user/user.service.ts:100
|
||||||||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async storeRefreshToken | |||||||||
storeRefreshToken(user: UserEntity, token: string)
|
|||||||||
Defined in apps/api/src/user/user.service.ts:84
|
|||||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async turnOfTwoFactorAuthentication | ||||||
turnOfTwoFactorAuthentication(user: UserEntity)
|
||||||
Defined in apps/api/src/user/user.service.ts:108
|
||||||
Parameters :
Returns :
Promise<UserEntity>
|
Async update | |||||||||
update(id: number, updates: UserUpdate)
|
|||||||||
Defined in apps/api/src/user/user.service.ts:36
|
|||||||||
Parameters :
Returns :
Promise<UserEntity>
|
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)
}
}