Skip to content

Commit b5672df

Browse files
committed
mock authentification added
1 parent 9eb0609 commit b5672df

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
simple authentication added, X-Password now combined to registered usernames:
2+
const registeredUsers = {
3+
"Ahmed": "ahmed-pass",
4+
"Emma": "emma-pass"
5+
}
6+
Example of request:
7+
curl -X POST --data '["Bees"]' -H "X-Username: Ahmed" -H "X-Password: ahmed-pass" http://localhost:3000

middleware-exercises/exercise-1/server.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ const port = process.env.PORT || 3000;
55
const app = express();
66
app.use(cors());
77

8+
const registeredUsers = {
9+
"Ahmed": "ahmed-pass",
10+
"Emma": "emma-pass"
11+
}
12+
813
function usernameMiddleware(req, res, next) {
9-
const username = req.get("X-Username");
10-
req.username = username ? username : null;
14+
const username = req.get("X-Username")? req.get("X-Username") : null;
15+
const password = req.get("X-Password");
16+
if (Object.keys(registeredUsers).includes(username) && password !== registeredUsers[username]){
17+
return res.status(400).send("Wrong credentials!");
18+
} else {
19+
req.username = username;
20+
}
1121
next();
1222
}
1323

@@ -44,7 +54,13 @@ function jsonBodyMiddleware(req, res, next) {
4454

4555
app.post("/", usernameMiddleware, jsonBodyMiddleware, (req, res) => {
4656
const { username, body } = req;
47-
let nameAnswer = "You are authenticated as " + username + ".";
57+
let nameAnswer = ''
58+
if (username !== null){
59+
nameAnswer = "You are authenticated as " + username + ".";
60+
} else {
61+
nameAnswer = "You are not authenticated";
62+
}
63+
4864
let answerStr = "You have requested information about " + body.length + " subjects: ";
4965
for (let i = 0; i < body.length; i++) {
5066
answerStr += body[i]

middleware-exercises/exercise-2/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@ In order to tell server to parse data as Json we have to add header to curl requ
33
"Content-Type: application/json"
44
Correct command for this case is:
55

6-
curl -X POST --data '["Bees"]' -H "Content-Type: application/json" -H "X-Username: Ahmed" http://localhost:3000
6+
curl -X POST --data '["Bees"]' -H "Content-Type: application/json" -H "X-Username: Ahmed" -H "X-Password: ahmed-pass" http://localhost:3000
7+
8+
simple authentication added, X-Password now combined to registered usernames:
9+
const registeredUsers = {
10+
"Ahmed": "ahmed-pass",
11+
"Emma": "emma-pass"
12+
}
713

middleware-exercises/exercise-2/server.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@ const app = express();
66
app.use(cors());
77
app.use(express.json());
88

9+
const registeredUsers = {
10+
"Ahmed": "ahmed-pass",
11+
"Emma": "emma-pass"
12+
}
13+
914
function usernameMiddleware(req, res, next) {
10-
const username = req.get("X-Username");
11-
req.username = username ? username : null;
15+
const username = req.get("X-Username")? req.get("X-Username") : null;
16+
const password = req.get("X-Password");
17+
if (Object.keys(registeredUsers).includes(username) && password !== registeredUsers[username]){
18+
return res.status(400).send("Wrong credentials!");
19+
} else {
20+
req.username = username;
21+
}
1222
next();
1323
}
1424

1525
app.post("/", usernameMiddleware, (req, res) => {
1626
const { username, body } = req;
17-
let nameAnswer = "You are authenticated as " + username + ".";
27+
let nameAnswer = ''
28+
if (username){
29+
nameAnswer = "You are authenticated as " + username + ".";
30+
} else {
31+
nameAnswer = "You are not authenticated";
32+
}
1833
let answerStr = "You have requested information about " + body.length + " subjects: ";
1934
for (let i = 0; i < body.length; i++) {
2035
answerStr += body[i]

0 commit comments

Comments
 (0)