Closed kevinschaich closed 6 months ago
Hey @kevinschaich, the posthog.identify
method in the posthog-node
SDK will only set user properties onto the person. It will not set any cookies. You'll need to use any of the client-side SDKs to set cookies.
Thanks for the reply @MarconLP. Are there any plans to change that, or at least a guide of how to correctly set / delete cookies to replicate the behavior of identify
in posthog-js
?
The reason I'm asking is server-side auth is becoming extremely popular nowadays with frameworks like Next.js / Nuxt / Svelte / Remix.
If all your auth logic (login / logout) is on the server, it doesn't make a ton of sense to handle the identify
call on the client, and you could potentially call it more than once by mistake.
Here's an example of what Supabase does with their server side lib to enable setting/getting/clearing cookies:
https://supabase.com/docs/guides/auth/server-side/oauth-with-pkce-flow-for-ssr
Are there any plans to change that, or at least a guide of how to correctly set / delete cookies to replicate the behavior of identify in posthog-js?
We do not plan to change that, since you'll still need to call .identify
at least once per session, to identify users who are coming back but are already logged in. You can solve the issue by running something like this every time your app is starting up:
if (isLoggedIn) {
posthog?.identify(id, {
name,
email,
subscriptionStatus,
});
} else {
if (posthog?._isIdentified()) { // only reset identified users to avoid resetting anonymous users every time this code runs
posthog?.reset()
}
}
The Node SDK is a stateless client, it's not caching any value so there's no need for reset (no cookies server side).
distinctId
is always a required attribute.
For example, capturing an event: https://posthog.com/docs/libraries/node#capturing-events
Identify a user https://posthog.com/docs/getting-started/user-properties#how-to-set-user-properties
Setting user attributes if you are already capturing an event https://posthog.com/docs/libraries/node#setting-user-properties
If you look at https://posthog.com/docs/getting-started/user-properties the method identify
isn't even mentioned for Node and it uses the capture
method instead with a $set
or $set_once
property which has the same effect of calling posthog.identify
the difference is if you are using capture
means that you are already capturing an event anyway and the identify
is to just identify the user but there's no meaningful event along with it.
So if you do:
client.capture({
distinctId: 'distinct_1',
event: 'event_name',
properties: {
$set: { name: 'Max Hedgehog' },
},
})
// and
client.capture({
distinctId: 'distinct_2',
event: 'event_name',
properties: {
$set: { name: 'Markus Hedgehog' },
},
})
you don't need to reset
anything since there are no cookies and what matters is the given distinctId
.
posthog.identify(
distinctId: 'distinct_1',
{ name: 'Max Hedgehog' }
);
// and
client.capture({
distinctId: 'distinct_2',
event: 'event_name',
properties: {
$set: { name: 'Markus Hedgehog' },
},
})
// same behavior if you call `identify`
Same since the distinctId
is different anyway.
Is your feature request related to a problem?
N/A
Describe the solution you'd like
posthog-node
has anidentify
function. It seems weird to me then that there's noreset
function as is available in the client lib.If we're doing server-side pages for
/login
and/logout
, how are we to reset props of the logged in user?Describe alternatives you've considered
Manually deleting Posthog cookies on logout
Related sub-libraries
Additional context
Thank you for your feature request – we love each and every one!