user.entity.ts67 lines, 1.4 KB
| 1 | import { |
| 2 | BeforeInsert, |
| 3 | BeforeUpdate, |
| 4 | Column, |
| 5 | CreateDateColumn, |
| 6 | Entity, |
| 7 | OneToMany, |
| 8 | PrimaryGeneratedColumn, |
| 9 | UpdateDateColumn, |
| 10 | } from "typeorm"; |
| 11 | import * as bcrypt from "bcrypt"; |
| 12 | import { Video } from "./video.entity"; |
| 13 | import { UserSubscription } from "./user-subscription.entity"; |
| 14 | |
| 15 | @Entity() |
| 16 | export class User { |
| 17 | @PrimaryGeneratedColumn("uuid") |
| 18 | id: string; |
| 19 | |
| 20 | @Column({ unique: true }) |
| 21 | email: string; |
| 22 | |
| 23 | @Column() |
| 24 | password: string; |
| 25 | |
| 26 | @Column({ nullable: true }) |
| 27 | name: string; |
| 28 | |
| 29 | @Column({ default: false }) |
| 30 | isEmailVerified: boolean; |
| 31 | |
| 32 | @Column({ nullable: true, type: "text" }) |
| 33 | emailVerificationToken: string | null; |
| 34 | |
| 35 | @Column({ nullable: true, type: "timestamp" }) |
| 36 | emailVerificationTokenExpires: Date | null; |
| 37 | |
| 38 | @Column({ nullable: true }) |
| 39 | lastLogin: Date; |
| 40 | |
| 41 | @OneToMany(() => Video, (video) => video.user) |
| 42 | videos: Video[]; |
| 43 | |
| 44 | @OneToMany(() => UserSubscription, (subscription) => subscription.user) |
| 45 | subscriptions: UserSubscription[]; |
| 46 | |
| 47 | @Column({ nullable: true }) |
| 48 | stripeCustomerId: string; |
| 49 | |
| 50 | @CreateDateColumn() |
| 51 | createdAt: Date; |
| 52 | |
| 53 | @UpdateDateColumn() |
| 54 | updatedAt: Date; |
| 55 | |
| 56 | @BeforeInsert() |
| 57 | @BeforeUpdate() |
| 58 | async hashPassword() { |
| 59 | if (this.password && this.password.length < 60) { |
| 60 | this.password = await bcrypt.hash(this.password, 10); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | async comparePassword(candidatePassword: string): Promise<boolean> { |
| 65 | return bcrypt.compare(candidatePassword, this.password); |
| 66 | } |
| 67 | } |