You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.1 KiB
45 lines
1.1 KiB
import * as bcrypt from "bcrypt"; |
|
import { omit } from "lodash"; |
|
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm"; |
|
|
|
import { IUser } from "@common/httpApi"; |
|
import { SprinklersDevice} from "./SprinklersDevice"; |
|
|
|
const HASH_ROUNDS = 1; |
|
|
|
@Entity() |
|
export class User implements IUser { |
|
@PrimaryGeneratedColumn() |
|
id!: number; |
|
|
|
@Column({ unique: true }) |
|
username: string = ""; |
|
|
|
@Column() |
|
name: string = ""; |
|
|
|
@Column() |
|
passwordHash: string = ""; |
|
|
|
@ManyToMany((type) => SprinklersDevice) |
|
@JoinTable({ name: "user_sprinklers_device" }) |
|
devices: SprinklersDevice[] | undefined; |
|
|
|
constructor(data?: Partial<User>) { |
|
if (data) { |
|
Object.assign(this, data); |
|
} |
|
} |
|
|
|
async setPassword(newPassword: string): Promise<void> { |
|
this.passwordHash = await bcrypt.hash(newPassword, HASH_ROUNDS); |
|
} |
|
|
|
async comparePassword(password: string): Promise<boolean> { |
|
return bcrypt.compare(password, this.passwordHash); |
|
} |
|
|
|
toJSON() { |
|
return omit(this, "passwordHash"); |
|
} |
|
}
|
|
|