File

apps/api/src/entities/user.entity.ts

Index

Properties

Properties

accessToken
Type : string
Decorators :
@Column({nullable: true})
active
Type : boolean
Decorators :
@Column({type: 'boolean', default: false})
confirmationCodes
Type : ConfirmCodeEntity[]
Decorators :
@OneToMany(undefined, confirmCode => confirmCode.user)
createdAt
Type : Date
Decorators :
@CreateDateColumn()
email
Type : string
Decorators :
@Column()
firstName
Type : string
Decorators :
@Column({nullable: true})
id
Type : number
Decorators :
@PrimaryGeneratedColumn()
isTwoFAEnabled
Type : boolean
Decorators :
@Column({type: 'boolean', default: false})
lastName
Type : string
Decorators :
@Column({nullable: true})
password
Type : string
Decorators :
@Column({nullable: true})
passwordResetCode
Type : string | null
Decorators :
@Column()
passwordResetExpiredTime
Type : string | null
Decorators :
@Column()
refreshToken
Type : string
Decorators :
@Column({nullable: true})
twoFASecret
Type : string | null
Decorators :
@Column()
updatedAt
Type : Date
Decorators :
@UpdateDateColumn()
import {
  BeforeInsert,
  BeforeUpdate,
  Column,
  CreateDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn
} from 'typeorm'
import { UserInterface } from '@isomera/interfaces'
import bcrypt from 'bcryptjs'
import { ConfirmCodeEntity } from './confirm-code.entity'
import { DateTime } from 'luxon'

@Entity({ name: 'users' })
export class UserEntity implements UserInterface {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ nullable: true })
  firstName: string

  @Column({ nullable: true })
  lastName: string

  @Column()
  email: string

  @Column({ nullable: true })
  password: string

  @Column({ nullable: true })
  accessToken: string

  @Column({ nullable: true })
  refreshToken: string

  @CreateDateColumn()
  createdAt: Date

  @UpdateDateColumn()
  updatedAt: Date

  @Column({ type: 'boolean', default: false })
  active: boolean

  @Column()
  passwordResetCode: string | null

  @Column()
  passwordResetExpiredTime: string | null

  @OneToMany(() => ConfirmCodeEntity, confirmCode => confirmCode.user)
  confirmationCodes: ConfirmCodeEntity[]

  @Column()
  twoFASecret: string | null

  @Column({ type: 'boolean', default: false })
  isTwoFAEnabled: boolean

  @BeforeInsert()
  @BeforeUpdate()
  async hashPassword(): Promise<void> {
    if (this.password) {
      const salt = await bcrypt.genSalt()
      if (!/^\$2[abxy]?\$\d+\$/.test(this.password)) {
        this.password = await bcrypt.hash(this.password, salt)
      }
    }
  }

  async checkPassword(plainPassword: string): Promise<boolean> {
    return await bcrypt.compare(plainPassword, String(this.password))
  }

  public isValidResetCodeTime() {
    const expiresAt = DateTime.fromFormat(
      this.passwordResetExpiredTime,
      'yyyy-MM-dd HH:mm:ss'
    )
    return DateTime.now() <= expiresAt
  }
}

results matching ""

    No results matching ""