Skip to content

Widget: setUser({ token }) should take a token function, like userToken #482

Closed

Description

@anurag629

Follow-up to #476. userToken in the widget config can now be a function the widget calls for a fresh token each time it asks for a session (UserToken in packages/widget/src/api/client.ts:6). ChatCops.setUser({ token }) still takes only a string (packages/widget/src/widget.ts:501, and the same signature in packages/widget/src/index.ts and ChatCopsLoader in packages/widget/src/loader/loader.ts). So a site that signs the visitor in after the page loads, which is what setUser is for, can't hand over a function.

What to do

  • Widen token in setUser to UserToken in all three places, and pass it through to client.setUserToken as it is.
  • setUser compares the new token with the old one to decide whether the visitor changed and the chat should be cleared (widget.ts:502-504). Two different functions are always different, so define what counts as the same visitor: the simplest rule is that passing the same function again doesn't clear the chat, and anything else does. Document it.

Acceptance criteria

  • Unit tests: setUser with a function, the function is called when a session is requested, the same function again doesn't clear the chat, and a different token or null does
  • Type tests in packages/widget/tests/api.test-d.ts for both builds
  • website/src/content/docs/widget/api.mdx (the setUser section) says it takes a function
  • A patch changeset for @chatcops/widget; pnpm test:coverage, pnpm lint and pnpm size pass

Comment here to be assigned.

Activity

  1. Sreehita-here commented on Oct 30, 2026

    @Sreehita-here
    Contributor

    Hey @anurag629 id like to work on this

  2. anurag629 commented on Oct 30, 2026

    @anurag629
    MemberAuthor

    Thanks @Sreehita-here, but this one is the follow-up @Saifjafri212213 asked for on #73 before I opened it, so it's his. I've just opened 485, a small one in the same area (the widget's CSP nonce). Comment there and it's yours.

  3. Saifjafri212213 commented on Oct 30, 2026

    @Saifjafri212213
    Collaborator

    Thanks @anurag629, I'd like to take this one. I have no open PRs or assigned issues in chatcops, so I have a free slot. Could you assign it to me? I'll start once it shows as assigned.

  4. anurag629 commented on Oct 30, 2026

    @anurag629
    MemberAuthor

    Assigned, @Saifjafri212213.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

hacktoberfestGood pick for Hacktoberfest contributors

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions