Next.js Discord

Discord Forum

What's the best way to get a users session cookie?

Unanswered
Chum salmon posted this in #help-forum
Open in Discord
Chum salmonOP
I'm trying to create a way for users to add reviews to item in my db. I have nearly all the functionality working I'm just not sure I understand the cookes() from next/headers.

Currently using this is a "use client" component
"use client"

import { cookies } from 'next/headers';

export default function Component() {
  const cookieStore = cookies();
  const sessionCookie = cookieStore.getAll()[1].value;

  return (
    // rest of the component
  )
}


Is this the best way to do this or should I look into another option? Ideally I would use the user's IP to prevent them from spamming reviews on more than 1 device but I'm not sure where to start with that either.

5 Replies

@Chum salmon I'm trying to create a way for users to add reviews to item in my db. I have nearly all the functionality working I'm just not sure I understand the `cookes()` from `next/headers`. Currently using this is a `"use client"` component tsx "use client" import { cookies } from 'next/headers'; export default function Component() { const cookieStore = cookies(); const sessionCookie = cookieStore.getAll()[1].value; return ( // rest of the component ) } Is this the best way to do this or should I look into another option? Ideally I would use the user's IP to prevent them from spamming reviews on more than 1 device but I'm not sure where to start with that either.
None of these methods is really useful against spamming. IP is dynamic for most people so they change with time and even if not, VPNs exist. Session cookies are even worse to identify and limit someone since I can just delete the cookie and get a new session in a matter of one second.

However, there's a couple methods you could use to get the user's IP depending on your current implementation.

Ideally you'd want them to only be able to comment when they are logged into an account.
Would this solution fit your project?
Chum salmonOP
Thank I'll take a look