So , I am making a blog type of website with MERN stack. I wanted to implement ‘like’ system like Facebook. But I don’t want visitors to signup to like on posts. Users should not be allowed to like again from the same Session or device. How do I accomplish this ?
Share
Just for simple case you can create a session ID on visit action and store that ID on local storage and limit the like action to 1 per session ID on a said post. This should basically do it but not in a full proof level.
post ma like property ni rakha , anonymous signin hanne ani harek choti click ma badhaudai lane
There are two ways to implement this.
If you think about what a `like` is – it is some sort of counter.
So a very basic/naive approach would be to create initialize counter starting from 0 – whenever a user likes a post – the counter increases (like++)
This is very basic implementation and has an issue – abuse – someone can `like` a post multiple times. You can create cookie tokens, ip tracking and other means to prevent but a simple counter is not ideal if you want to track who liked.
So how about an array? You can store `id` of each logged in user that `liked` the post. So the number of likes is just the length of array. You can easily view who liked and how many are there easily.