launchdarkly-labs / ld-nextjs

Experimental repo to integrate LD with the Next.js App Router.
Apache License 2.0
2 stars 0 forks source link

Simple .identify example for new context (context change) #3

Open codyde opened 1 week ago

codyde commented 1 week ago

I tried to write this up real quick, but I couldnt' figure out how to expose the .identify method from the client side launchdarkly client.

I think we should show a "Login button" example in the client component that when a user "clicks" - it changes the context from the default anonymous one, and updates the context in the cookie. We would then show how the new context is passed to the server side via cookies as well, creating a consistent login.

I tried to do this in this PR - https://github.com/launchdarkly-labs/ld-nextjs/pull/2, but ultimately removed the code because I couldn't get it working quite right.

yusinto commented 1 week ago

I think you're right we should support identify. In a Client Component, we can cast the ldClient returned from useLDClient as JSSdk and call the identify function onButtonClick. That should do it. This code should work with ssr because the button click event will not be called on server render. It will only execute on the browser. I'll try do it in js-core tomorrow.