Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRodrigo Muñoz
Courses Plus Student 20,171 PointsHow can I make an upvote system in which only accepts 1 vote per user on each post?
I've seen the demo but it allows to make infinite votes to the posts. How can I make that only accepts 1 vote per user on each post?
3 Answers
Kevin Korte
28,149 PointsAre you going to be doing user accounts at all? That's the most bullet proof way, but even than it lends itself to abuse. You can also use cookies, local storage, or session variables, but those can be worked around with incognito sessions, or different browsers, or in the case of cookies and local storage, the user can simply delete the record from their browser, and vote again!
Fernando Nazario
5,553 PointsYou can use User Authentication and Authorization, and create a logic of one vote per User (var has_voted = false; if clicked { has_voted = true }; if !has_voted { let the user vote }; something like that ). There is a course of User authentication and authorization using Node.js and Express in Treehouse
eck
43,038 PointsThere are two things that you will need to be able to do to pull this off:
- Identify users.
- Keep track of which answers they upvote.
Kevin and Fernando both mention some ways you could identify a user, like with sessions or user authentication.
Whether you have a session or a user to identify the visitors to your website, you will need a way to store questions and their properties, such as votes. This will likely involve some manner of database, from which there are many you can choose. One option is to use MongoDB
for you database. From here you could create a collection
of questions with a field for votes. This field could store who has voted in an array
, which you can check to confirm new voters.
If you wanted to learn more about how to pull that off Treehouse has a great tutorial for setting up user authentication with express and MongoDB that would have a lot of relevant information and serve as a good starting point.