All questions

Where do I call identify on login in the App Router, server or client?

Asked by jasper88 on

Jjasper88

I want logged-in users recognized so their profile carries across visits. What I cannot figure out is where identify belongs in the App Router, a server action or a client component.

While testing i kept getting an "Invalid user ID" error and i am not sure if thats a placement problem or teh value i am passing. Where do people wire this?

Was this helpful?

2 answers

TATom Alvarez

Call it when the login actually resolves on the client, tied to the point where you know who the user is. croct.identify('<userId>') takes a string user ID and only a string. That "Invalid user ID" error is almost always a non-string sneaking in, a number, an object, a null, so coerce it to a string first.

Identity resolution then links the anonymous profile the visitor already built up to the identified one, so you do not lose the earlier activity when they log in. One config note while you are here: the userId and token init options are mutually exclusive, pick one path and do not set both.

Was this helpful?
ASAline Souza

please do not forget the other side, on logout you call croct.anonymize(). it mirrors the identify call. otherwise the session stays attached to the identified user after they leave. in our case we did so and it kept the handoff between logged in and logged out clean.

Was this helpful?
Jjasper88

it was the value, i was passing the numeric id straight from our db. wrapping it in String() cleared the error immediately. adding anonymize on logout too, thanks both

Still have questions?