Skip to content

Commit a8d6649

Browse files
committed
store hashed password in the database
1 parent b8f8e24 commit a8d6649

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

Projects/BooksLibrary/package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Projects/BooksLibrary/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "akash jadhav",
1313
"license": "ISC",
1414
"devDependencies": {
15+
"@types/bcryptjs": "^2.4.2",
1516
"@types/cors": "^2.8.12",
1617
"@types/express": "^4.17.13",
1718
"@types/express-fileupload": "^1.2.2",
@@ -28,6 +29,7 @@
2829
"typescript": "^4.6.2"
2930
},
3031
"dependencies": {
32+
"bcryptjs": "^2.4.3",
3133
"cors": "^2.8.5",
3234
"dotenv": "^16.0.0",
3335
"express": "^4.17.3",

Projects/BooksLibrary/src/common/helper.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import { InputValidationError } from './input.validation.error';
2+
import { compareSync, genSaltSync, hashSync } from 'bcryptjs';
23

34
export class Helper {
5+
public static hash = (str: string) => {
6+
const salt = genSaltSync(8);
7+
const hashed = hashSync(str, salt);
8+
return hashed;
9+
};
10+
11+
public static compare = (plainText: string, hashed: string) => {
12+
return compareSync(plainText, hashed);
13+
};
14+
415
static handleValidationError = (result) => {
516
let index = 1;
617
const errorMessages = [];

Projects/BooksLibrary/src/database/sql/sequelize/models/user.model.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/* eslint-disable indent */
2+
import { Helper } from 'common/helper';
23
import {
4+
BeforeCreate,
5+
BeforeUpdate,
36
BelongsTo,
47
Column,
58
CreatedAt,
@@ -81,6 +84,12 @@ export default class User extends Model {
8184
})
8285
Password: string;
8386

87+
@BeforeCreate
88+
@BeforeUpdate
89+
static encryptPassword(client) {
90+
client.Password = Helper.hash(client.Password);
91+
}
92+
8493
// @IsUUID(4)
8594
@ForeignKey(() => Role)
8695
@Column({

0 commit comments

Comments
 (0)