I wonder why this error comes, I can register and login the user succesfully, even control goes upto next in customer/auth but after next, postman shows this, please help](https://i.sstatic.net/khURopb8.png)
This is index.js
const express = require('express');
const jwt = require('jsonwebtoken');
const session = require('express-session')
const customer_routes = require('./router/auth_users.js').authenticated;
const genl_routes = require('./router/general.js').general;
const app = express();
app.use(express.json());
app.use("/customer",session({secret:"fingerprint_customer",resave: true, saveUninitialized: true}))
app.use("/customer/auth/*", function auth(req,res,next){
// middleware for authentication, firstly check for authentication and then pass control for next task...
if(req.session.authorization) {
let token = req.session.authorization['accessToken']; // Access Token
jwt.verify(token, "access",(err,user)=>{
if(!err){
req.user = user;
next();
}
else{
return res.status(403).json({message: "User not authenticated"})
}
});
} else {
return res.status(403).json({message: "User not logged in"})
}
});
`const PORT =5000;
app.use("/customer", customer_routes);
app.use("/", genl_routes);
app.listen(PORT,()=>console.log("Server is running"));
`
and this is auth_users.js
const express = require('express');
const jwt = require('jsonwebtoken');
let books = require("./booksdb.js");
const regd_users = express.Router();
let users = [];
//Check, is the username valid
const isValid = (username)=>{ //returns boolean
let userswithsamename = users.filter((user)=>{
return user.username === username
});
if(userswithsamename.length > 0){
return true;
} else {
return false;
}
}
//Check if username and password match the one we have in records.
const authenticatedUser = (username,password)=>{ //returns boolean
let validusers = users.filter((user)=>{
return (user.username === username && user.password === password)
});
if(validusers.length > 0){
return true;
} else {
return false;
}
}
//only registered users can login
regd_users.post("/login", (req,res) => {
const username = req.body.username;
const password = req.body.password;
if (!username || !password) {
return res.status(404).json({message: "Error logging in"});
}
if (authenticatedUser(username,password)) {
let accessToken = jwt.sign({
data: password
}, 'access', { expiresIn: 60 * 60 });
req.session.authorization = {
accessToken,username
}
return res.status(200).send("User successfully logged in");
} else {
return res.status(208).json({message: "Invalid Login. Check username and password"});
}
});
// Add a book review
regd_users.put("/auth/review/:isbn", (req, res) => {
console.log("hiiii");
const isbn = req.params.isbn;
const { review } = req.body;
const username = req.session.username;
if (!username) {
res.status(401).json({ error: "User not logged in." });
return;
}
if (!review) {
res.status(400).json({ error: "Review is required." });
return;
}
if (!books[isbn]) {
res.status(404).json({ error: "Book not found." });
return;
}
const reviewObject = {
username: username,
review: review
};
if (!books[isbn].reviews) {
// If the user hasn't posted a review for this book before, create a new review entry
books[isbn].reviews = reviewObject;
res.status(201).json({ message: "Review posted successfully." });
} else {
// If the user has already posted a review for this book, update their existing review
books[isbn].reviews = reviewObject;
res.status(200).json({ message: "Review updated successfully." });
}
});
module.exports.authenticated = regd_users;
module.exports.isValid = isValid;
module.exports.users = users;
I tried changing path to customer/auth/review? (without slash), also without customer and did everything I could but the result is always same. Please help.`
2
Answers
Based on your code your URL should look like this:
/customer/auth/review/AS1
When defining the routes you are setting ‘isbn’ as a URL parameter but in Postman you’re trying to use it as a query string,
In Postman You are adding
isbn
inParams
section which is available byreq.query
object .So remove this line of code.And add this line instead..
This might solve your problem.try this once.