Skip to content

Commit 08ebdee

Browse files
committed
installed bcrypt and implemented funcs to hash and verify passwords
1 parent 0828694 commit 08ebdee

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

shatter-backend/package-lock.json

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

shatter-backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
"license": "ISC",
1414
"description": "",
1515
"dependencies": {
16+
"bcryptjs": "^3.0.3",
1617
"dotenv": "^17.2.3",
1718
"express": "^5.1.0",
1819
"mongoose": "^8.19.2",
1920
"zod": "^4.1.12"
2021
},
2122
"devDependencies": {
2223
"@eslint/js": "^9.38.0",
24+
"@types/bcryptjs": "^2.4.6",
2325
"@types/express": "^5.0.5",
2426
"@types/node": "^24.9.2",
2527
"eslint": "^9.38.0",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import bcrypt from 'bcryptjs';
2+
3+
// How many times to scramble the password (10 seems to be the standard)
4+
// the password will be hashed 2^10 = 1024 times
5+
const SALT_ROUNDS = 10
6+
7+
// we use export so other files can import and use this func
8+
// hashing takes quite some time so we use the async keyword
9+
// the function will return a promise that resolves to a string (hash)
10+
export const hashPassword = async (password: string): Promise<string> => {
11+
try {
12+
// 1- generate a random salt (unique random data)
13+
// 'await' ensures that this assignment is complete before moving on
14+
const salt = await bcrypt.genSalt(SALT_ROUNDS)
15+
16+
// 2- combine password with salt and hash it
17+
const hash = await bcrypt.hash(password, salt);
18+
19+
// return the hash (this is what we store in database)
20+
return hash;
21+
} catch (error) {
22+
console.error('Error hashing password:', error);
23+
throw new Error('Failed to hash password');
24+
}
25+
};
26+
27+
// takes the password user typed and stored hash
28+
// recall that bcrypt stores the salt inside the hash
29+
// the function internally extracts salt from hash
30+
// it then rehashes the typed password with same salt and compares
31+
export const comparePassword = async (
32+
password: string,
33+
hash: string,
34+
): Promise<boolean> => {
35+
try {
36+
// bcrypt extracts the salt from the hash and compares
37+
const isMatch = await bcrypt.compare(password, hash);
38+
return isMatch;
39+
} catch (error) {
40+
console.error('Error comparing passwords:', error);
41+
throw new Error('Failed to compare passwords');
42+
}
43+
};
44+
45+

0 commit comments

Comments
 (0)