Skip to content

feat(widget): setUser({ token }) takes a token function, like userToken - #491

Merged
anurag629 merged 2 commits into
codercops:devfrom
Saifjafri212213:feat/widget-setuser-token-function
Oct 30, 2026
Merged

anurag629 merged 2 commits into
codercops:devfrom
Saifjafri212213:feat/widget-setuser-token-function

Conversation

@Saifjafri212213

Copy link
Copy Markdown
Collaborator

Summary

ChatCops.setUser({ token }) now takes a token function, the same as userToken. A site that signs the visitor in after the page loads can hand over a function, so the token is signed when the widget asks for a session and cannot have expired by then.

Closes #482

What changed

  • token in setUser() is now a UserToken (a string, or a sync or async function that returns a token) in all three places: Widget.setUser (widget.ts), ChatCops.setUser (index.ts) and ChatCopsLoader.setUser (loader.ts). It is passed to client.setUserToken as it is, and the client already resolves a function in the /session request (feat(widget): userToken can be a function that returns a fresh token #476), so no other code changed.
  • Same visitor or another one: setUser compares the new token with the old one by identity (!==), so passing the same function again keeps the chat, and a different function, a string, no token or null counts as another visitor and clears it. That was already how the comparison worked for strings; the change is documenting and testing it for functions. A visitor who was anonymous before keeps the chat when they sign in, as before.
  • widget/api.mdx (the setUser section) says it takes a function, what counts as the same visitor, and that the function reference has to be kept if setUser is called more than once. api/widget.api.md is regenerated, and there is a patch changeset.

Notes for review

  • An arrow function written inline in each setUser call is a different function every time, so it clears the chat. The docs say to keep one reference. If you would rather treat any two functions as the same visitor, it is a one-line change, but then switching users with two functions could not be told apart.
  • No size change: the panel is still 17.15 kB of 18 kB and the single file 19.24 kB of 20 kB, since only types changed.

How I Tested This

  • Wrote the tests first: the type tests failed on the old code, and the runtime tests passed only after the type change compiled.
  • tests/user-token.test.ts: the function is called when a session is requested and not when setUser is called; the same function again keeps the chat; a different function, a string, undefined and null each clear it; a user with no token clears it; an anonymous visitor keeps the chat when signing in with a function; ChatCops.setUser in the npm and single-file builds; a later string replaces the function.
  • tests/api.test.ts: the script tag loader keeps a function given to setUser until the chat loads and passes it on unchanged, without calling it.
  • tests/api.test-d.ts: setUser takes a string or a function in both builds, and a function that returns a number is rejected.
  • Mutated setUser in four ways (always clear, never clear, clear when anonymous, token not passed on); a test fails for each.
  • 777 widget tests pass with coverage, typecheck is clean, lint is at 39 warnings (the same as dev), pnpm size is within the limits, and pnpm docs:check finds no errors in the widget docs. The whole e2e suite passes except the three S7 reply with sources tests, which fail on a clean dev here too. Other packages' docs samples fail the check here for missing builds.

AI assistance

An AI coding assistant helped write the change and the tests. I ran the checks listed above.

…en (codercops#482)

The token in setUser(), ChatCops.setUser() and the script tag loader is now a
UserToken: a string, or a function (sync or async) that returns a fresh token
each time a session is requested. Passing the same function again keeps the
chat; a different function, a string or no token counts as another visitor.

@anurag629 anurag629 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Saifjafri212213, this covers #482. The type is widened in all three places, the rule (the same function is the same visitor, anything else isn't) is in the doc comment and on api.mdx, and the tests cover each case from the issue plus anonymous-to-function and function-to-string. With current dev merged in: build, typecheck, test:coverage, lint (same as dev), api:check, docs:check and size pass. Merging once CI is green.

@anurag629
anurag629 merged commit 6696a21 into codercops:dev Oct 30, 2026
@anurag629 anurag629 added the hacktoberfest-accepted PR counts for Hacktoberfest label Oct 30, 2026
@anurag629

Copy link
Copy Markdown
Member

Merged, thanks @Saifjafri212213.

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

Labels

hacktoberfest-accepted PR counts for Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants