What's the best way to get a users session cookie?
Unanswered
Chum salmon posted this in #help-forum
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
Currently using this is a
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.
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?
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?
@ncls. 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
I'm not worried about All spam, just want to make it slow down a user from adding one after another. Could you point me in the right direction on how I could use the Users IP?
I'd like to stay clear of user accounts and logging in if possible
@Chum salmon I'm not worried about All spam, just want to make it slow down a user from adding one after another. Could you point me in the right direction on how I could use the Users IP?
Usually it's simply
<NextRequest>.ip. You gotta check if that fits your implementation.Chum salmonOP
Thank I'll take a look