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 trial

JavaScript Build a REST API With Express Building a REST Service Introducing REST APIs

How 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
Kevin Korte
28,148 Points

Are 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
Fernando Nazario
5,553 Points

You 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

There are two things that you will need to be able to do to pull this off:

  1. Identify users.
  2. 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.