Users

In this chapter, we will go through the Users Module and give you an overview of how module works

Users module path

├── node_modules
├── mail-templates
├── migrations
├── src
│   ├── configs
│   ├── domains
│   │   ├── users             # Users module

User entity

@Entity()
export class User extends BaseEntity {
  // before insert and update hooks
  @BeforeInsert()
  @BeforeUpdate()
  async hashPassword() {
    if (!!this.password) {
      if (!this.password.includes('$2b$10')) {
        this.password = bcrypt.hashSync(this.password, 10);
      }
    }
  }

  @BeforeInsert()
  @BeforeUpdate()
  lowerCaseEmail() {
    this.email = this.email.toLowerCase();
  }

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  isAdmin: boolean;

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

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

  @Index({ unique: true })
  @Column()
  email: string;

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

  @Column({ default: null })
  resetPasswordToken: string;

  @Column({ default: false })
  confirmPayment: boolean;

  @Column({ default: false })
  errorPayment: boolean;

  @Column({ nullable: true })
  customerStripeId: string;

  @Column({ nullable: true })
  @IsOptional()
  endSubscription: Date;

  @Column({ default: false })
  canceledSubscription: boolean;

  @Column({ nullable: true})
  magicToken: string;

  @Column({ nullable: true })
  planId: number;

  @ManyToOne(() => Plan, plan => plan.users)
  @JoinColumn()
  plan: Plan;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}


export function password(length): string {
    let pass = '';
    for (let l=0; l < length; l++) {
        const rand = Math.random() * (126 - 33) + 33;
        pass += String.fromCharCode(~~rand);
    }
    return pass;
}

Users routes

Last updated