Skip to content

Commit 323d4d3

Browse files
Final Project
1 parent fa6bb07 commit 323d4d3

File tree

4 files changed

+303
-44
lines changed

4 files changed

+303
-44
lines changed

final_project/index.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,39 @@ const jwt = require('jsonwebtoken');
33
const session = require('express-session')
44
const customer_routes = require('./router/auth_users.js').authenticated;
55
const genl_routes = require('./router/general.js').general;
6+
const { users } = require("./router/auth_users.js");
67

78
const app = express();
89

910
app.use(express.json());
1011

11-
app.use("/customer",session({secret:"fingerprint_customer",resave: true, saveUninitialized: true}))
12+
app.get("/users", (req, res) => {
13+
return res.status(200).json({
14+
users,
15+
});
16+
});
17+
18+
app.use("/customer", session({ secret: "fingerprint_customer", resave: true, saveUninitialized: true }))
19+
20+
app.use("/customer/auth/*", function auth(req, res, next) {
21+
if (!req.session.authorization) {
22+
return res.status(403).json({ message: "User not logged in" });
23+
}
1224

13-
app.use("/customer/auth/*", function auth(req,res,next){
14-
//Write the authenication mechanism here
25+
const token = req.session.authorization.accessToken;
26+
27+
jwt.verify(token, "fingerprint", (err, user) => {
28+
if (err) {
29+
return res.status(403).json({ message: "Invalid token" });
30+
}
31+
req.user = user;
32+
next();
33+
});
1534
});
16-
17-
const PORT =5000;
35+
36+
const PORT = 5000;
1837

1938
app.use("/customer", customer_routes);
2039
app.use("/", genl_routes);
2140

22-
app.listen(PORT,()=>console.log("Server is running"));
41+
app.listen(PORT, () => console.log("Server is running"));

final_project/router/auth_users.js

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,99 @@ const regd_users = express.Router();
66
let users = [];
77

88
const isValid = (username)=>{ //returns boolean
9-
//write code to check is the username is valid
9+
return users.some((user) => user.username === username);
1010
}
1111

1212
const authenticatedUser = (username,password)=>{ //returns boolean
13-
//write code to check if username and password match the one we have in records.
13+
return users.some(
14+
(user) => user.username === username && user.password === password
15+
);
1416
}
1517

1618
//only registered users can login
17-
regd_users.post("/login", (req,res) => {
18-
//Write your code here
19-
return res.status(300).json({message: "Yet to be implemented"});
19+
regd_users.post("/login", (req, res) => {
20+
const { username, password } = req.body;
21+
22+
if (!username || !password) {
23+
return res.status(409).json({
24+
message: "Please enter username and password both!",
25+
});
26+
}
27+
28+
if (!authenticatedUser(username, password)) {
29+
return res.status(400).json({
30+
message: "Username and password do not match",
31+
username,
32+
password,
33+
});
34+
}
35+
36+
const token = jwt.sign({ username }, "fingerprint", { expiresIn: "1h" });
37+
38+
req.session.authorization = {
39+
accessToken: token,
40+
username,
41+
};
42+
43+
return res.status(200).json({
44+
message: "Login successful",
45+
token: token,
46+
});
2047
});
2148

2249
// Add a book review
2350
regd_users.put("/auth/review/:isbn", (req, res) => {
24-
//Write your code here
25-
return res.status(300).json({message: "Yet to be implemented"});
51+
const isbn = req.params.isbn;
52+
const review = req.body.review;
53+
54+
if (!review) {
55+
return res.status(400).json({ message: "Review is required" });
56+
}
57+
58+
const username = req.session.authorization?.username;
59+
const book = books[isbn];
60+
61+
if (!book) {
62+
return res.status(404).json({ message: "Book not found" });
63+
}
64+
65+
book.reviews[username] = review;
66+
67+
return res.status(200).json({
68+
message: "Rating saved",
69+
reviews: book.reviews,
70+
books
71+
});
72+
});
73+
74+
regd_users.delete("/auth/review/:isbn", (req, res) => {
75+
const isbn = req.params.isbn; // keep as string
76+
const username = req.session.authorization?.username;
77+
78+
if (!username) {
79+
return res.status(401).json({
80+
message: "Not authorized. Please log in",
81+
});
82+
}
83+
84+
const book = books[isbn];
85+
if (!book) {
86+
return res.status(404).json({
87+
message: "Book not found",
88+
});
89+
}
90+
91+
if (!book.reviews[username]) {
92+
return res.status(404).json({
93+
message: "No review found from this user",
94+
});
95+
}
96+
97+
delete book.reviews[username]; // delete user’s review
98+
99+
return res.status(200).json({
100+
message: "Review successfully deleted",
101+
});
26102
});
27103

28104
module.exports.authenticated = regd_users;

final_project/router/booksdb.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,54 @@
11
let books = {
2-
1: {"author": "Chinua Achebe","title": "Things Fall Apart", "reviews": {} },
3-
2: {"author": "Hans Christian Andersen","title": "Fairy tales", "reviews": {} },
4-
3: {"author": "Dante Alighieri","title": "The Divine Comedy", "reviews": {} },
5-
4: {"author": "Unknown","title": "The Epic Of Gilgamesh", "reviews": {} },
6-
5: {"author": "Unknown","title": "The Book Of Job", "reviews": {} },
7-
6: {"author": "Unknown","title": "One Thousand and One Nights", "reviews": {} },
8-
7: {"author": "Unknown","title": "Nj\u00e1l's Saga", "reviews": {} },
9-
8: {"author": "Jane Austen","title": "Pride and Prejudice", "reviews": {} },
10-
9: {"author": "Honor\u00e9 de Balzac","title": "Le P\u00e8re Goriot", "reviews": {} },
11-
10: {"author": "Samuel Beckett","title": "Molloy, Malone Dies, The Unnamable, the trilogy", "reviews": {} }
12-
}
2+
1: {
3+
author: "Chinua Achebe",
4+
title: "Things Fall Apart",
5+
reviews: {},
6+
},
7+
2: {
8+
author: "Hans Christian Andersen",
9+
title: "Fairy tales",
10+
reviews: {},
11+
},
12+
3: {
13+
author: "Dante Alighieri",
14+
title: "The Divine Comedy",
15+
reviews: {},
16+
},
17+
4: {
18+
author: "Unknown",
19+
title: "The Epic Of Gilgamesh",
20+
reviews: {},
21+
},
22+
5: {
23+
author: "Unknown",
24+
title: "The Book Of Job",
25+
reviews: {},
26+
},
27+
6: {
28+
author: "Unknown",
29+
title: "One Thousand and One Nights",
30+
reviews: {},
31+
},
32+
7: {
33+
author: "Unknown",
34+
title: "Nj\u00e1l's Saga",
35+
reviews: {},
36+
},
37+
8: {
38+
author: "Jane Austen",
39+
title: "Pride and Prejudice",
40+
reviews: {},
41+
},
42+
9: {
43+
author: "Honor\u00e9 de Balzac",
44+
title: "Le P\u00e8re Goriot",
45+
reviews: {},
46+
},
47+
10: {
48+
author: "Samuel Beckett",
49+
title: "Molloy, Malone Dies, The Unnamable, the trilogy",
50+
reviews: {},
51+
},
52+
};
1353

14-
module.exports=books;
54+
module.exports = books;

0 commit comments

Comments
 (0)