Skip to content

Commit 524052d

Browse files
committed
Typescript Bootcamp
1 parent 745cf36 commit 524052d

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

rest-api/src/models/delete-db.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "reflect-metadata";
66
import {AppDataSource} from "../data-source";
77
import {Lesson} from "./lesson";
88
import {Course} from "./course";
9+
import {User} from "./user";
910

1011
async function deleteDb() {
1112

@@ -21,6 +22,10 @@ async function deleteDb() {
2122

2223
await AppDataSource.getRepository(Course).delete({});
2324

25+
console.log(`Clearing USERS table.`);
26+
27+
await AppDataSource.getRepository(User).delete({});
28+
2429
}
2530

2631

rest-api/src/models/populate-db.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const result = dotenv.config();
55

66
import "reflect-metadata";
77

8-
import {COURSES} from "./db-data";
8+
import {COURSES, USERS} from "./db-data";
99
import {AppDataSource} from "../data-source";
1010
import { Course } from "./course";
1111
import {DeepPartial} from "typeorm";
1212
import {Lesson} from "./lesson";
13+
import {User} from "./user";
14+
import {calculatePasswordHash} from "../utils";
1315

1416
async function populateDb() {
1517

@@ -44,6 +46,29 @@ async function populateDb() {
4446

4547
}
4648

49+
const users = Object.values(USERS) as any[];
50+
51+
for (let userData of users) {
52+
53+
console.log(`Inserting user: ${userData}`);
54+
55+
const {email, pictureUrl, isAdmin, passwordSalt, plainTextPassword} = userData;
56+
57+
const user = AppDataSource
58+
.getRepository(User)
59+
.create({
60+
email,
61+
pictureUrl,
62+
isAdmin,
63+
passwordSalt,
64+
passwordHash: await calculatePasswordHash(
65+
plainTextPassword, passwordSalt)
66+
});
67+
68+
await AppDataSource.manager.save(user);
69+
70+
}
71+
4772
const totalCourses = await courseRepository
4873
.createQueryBuilder()
4974
.getCount();

rest-api/src/models/user.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export class User {
1515
@Column()
1616
passwordHash:string;
1717

18+
@Column()
19+
passwordSalt:string;
20+
1821
@Column()
1922
pictureUrl:string;
2023

rest-api/src/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11

2+
const crypto = require("crypto");
3+
const util = require("util");
24

5+
const hashPassword = util.promisify(crypto.pbkdf2);
36

47
export function isInteger(input:string) {
58
return input?.match(/^\d+$/) ?? false;
69
}
10+
11+
12+
export async function calculatePasswordHash(
13+
plainTextPassword:string,
14+
passwordSalt:string) {
15+
16+
const passwordHash = await hashPassword(
17+
plainTextPassword,
18+
passwordSalt,
19+
1000,
20+
64,
21+
"sha512");
22+
23+
return passwordHash.toString("hex");
24+
}

0 commit comments

Comments
 (0)