I'm currently working on a solution to display the correct user information on a page when users navigate to "/users/:name". For example, I want to show "welcome user2" if user2 is logged in. My approach involves passing along the parameter from "/users/:name" and checking if it matches the username value. However, I am unsure if this method is secure. How can I iterate through my list of objects and compare them to the parameter?
When I receive this data in my jade document:
{ list: 'userList', users: [ { password: 'pass1', username: 'user1' }, { username: 'user2', password: 'pass2' }, { username: 'user3', password: 'pass3' } ], address: '14459 70th St City NY', desc: '3 floors', __v: 0, _id: 56baf181356641f01213295a }
This data is sent because of the following code:
app.get("/users/:name", function(req, res){
// console.log(req.params.name)
User.findOne({"users": { $elemMatch: { username: req.params.name}}}, function(err, doc){
console.log("test ", doc)
res.render("users", {result : doc, name: req.params.name});
})
})
In Jade:
html
head
body
p= result
p Welcome #{result.users[0].username} #{name} // prints out--> Welcome user1 user2 ||| when user2 signs in
p= result.address
h3= result.desc
a(href="/logout") logout