I have a simple web application that allows for registering of new users using bcrypt to store the password.
My TypeORM User entity looks like:
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({
type: 'nvarchar',
length: 256,
nullable: false,
unique: true,
})
username: string;
@Column({
type: 'nvarchar',
length: 256,
nullable: false,
})
password: string;
@BeforeInsert() async hashPassword() {
this.password = await bcrypt.hash(this.password, 10); // salt rounds
}
async comparePasswordAsync(attempt: string): Promise<boolean> {
return await bcrypt.compare(attempt, this.password);
}
}
The creation of new users using an exposed endpoint works fine. Now, let's say I want to ship the product with a default admin account in the User table.
How do I write a migrator such that it adds a default username and password for an admin account?
Here is my migrator:
export class UserTable1594240665620 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// Create the user table
await queryRunner.query(`
CREATE TABLE user(
id VARCHAR(36) PRIMARY KEY NOT NULL,
username VARCHAR(256) UNIQUE NOT NULL,
password VARCHAR(256) NOT NULL,
);`);
// Add the default user
await queryRunner.query(`INSERT INTO user(id, username, password)
VALUES (UUID(), 'defaultAdmin', ????);
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE user`);
}
}
What do I put into the ???
in the above code in order to save the hashed default admin password?