Skip to content

Commit 8dabd41

Browse files
committed
fix: add bcrypt
1 parent 0f3b290 commit 8dabd41

File tree

5 files changed

+559
-7
lines changed

5 files changed

+559
-7
lines changed

playground/27-rest-api/app.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const bodyParser = require("body-parser");
55
const Joi = require("joi");
66
const fs = require("fs");
77
const crypto = require("crypto");
8+
const bcrypt = require("bcrypt");
89
require("dotenv").config();
910

1011
const app = express();
@@ -145,10 +146,13 @@ app.post("/signup", (req, res) => {
145146
return res.status(400).json({ error: "Username already taken" });
146147
}
147148

149+
// Hash the password
150+
const hashedPassword = bcrypt.hashSync(password, 10);
151+
148152
const newUser = {
149153
id: crypto.randomUUID(),
150154
username,
151-
password,
155+
password: hashedPassword,
152156
};
153157

154158
users.push(newUser);
@@ -159,11 +163,9 @@ app.post("/signup", (req, res) => {
159163

160164
app.post("/login", (req, res) => {
161165
const { username, password } = req.body;
162-
const user = users.find(
163-
(u) => u.username === username && u.password === password
164-
);
166+
const user = users.find((u) => u.username === username);
165167

166-
if (!user) {
168+
if (!user || !bcrypt.compareSync(password, user.password)) {
167169
return res.status(401).json({ error: "Invalid credentials" });
168170
}
169171

playground/27-rest-api/meetups.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
"title": "Dummy Meetup",
55
"summary": "An updated dummy meetup for testing purposes",
66
"address": "456 Elm St"
7+
},
8+
{
9+
"id": "5091638d-d616-4d95-9570-0e8f16fabea2",
10+
"title": "Tech Conference",
11+
"summary": "A conference for tech enthusiasts and professionals",
12+
"address": "123 Main St"
713
}
814
]

0 commit comments

Comments
 (0)