Skip to content

completed task7,8,9 #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions final_project/router/auth_users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,68 @@ const regd_users = express.Router();

let users = [];

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

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

//only registered users can login
regd_users.post("/login", (req,res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const {username, password} = req.body;
if(!username || !password){
return res.status(400).json({message: "username and password required"});
}
if(authenticatedUser(username, password)){
const token = jwt.sign({username: username}, "secret", {expiresIn: "1h"});
req.session.authorization ={
token,
username,
};
return res.status(200).json({message: "user logged in successfully"]);
}else{
return res.status(401).json({message: "Invalid login credentials"});
}

});

// Add a book review
regd_users.put("/auth/review/:isbn", (req, res) => {
//Write your code here
return res.status(300).json({message: "Yet to be implemented"});
const isbn = req.params.isbn;
const review = req.query.review;
const username = req.session.authorization?.username;
if(!username){
return res.status(401).json({message: " User not logged in"});
}
if(!books[isbn]){
return res.status(404).json({message: "Book not found"});
}
books[isbn].reviews[username] = review;
return res.status(200).json({message: "Review added/modified successfully"});

});

//delete a book review
regd_users.delete("/auth/review/:isbn", (req, res) => {
const isbn = req.params.isbn;
const username = req.session.authorization?.username;
if(!username){
return res.status(401).json({message: User not logged in"});
}
if(!books[isbn]){
return res.status(404).json({message: "Book not found"});
}
const bookReviews = books[isbn].reviews;
if(bookReviews[username]){
delete bookReviews[username];
return res.status(200).json({message: "Review deleted successfully"});
}else{
return res.status(404).json({message: "Review not found for this user"});
});

module.exports.authenticated = regd_users;
module.exports.isValid = isValid;
module.exports.users = users;