Repository navigation
feat(widget): setUser({ token }) takes a token function, like userToken - #491
Merged
anurag629 merged 2 commits intoOct 30, 2026
Merged
Conversation
…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
approved these changes
Oct 30, 2026
anurag629
left a comment
Member
There was a problem hiding this comment.
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.
Member
|
Merged, thanks @Saifjafri212213. |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ChatCops.setUser({ token })now takes a token function, the same asuserToken. 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
tokeninsetUser()is now aUserToken(a string, or a sync or async function that returns a token) in all three places:Widget.setUser(widget.ts),ChatCops.setUser(index.ts) andChatCopsLoader.setUser(loader.ts). It is passed toclient.setUserTokenas it is, and the client already resolves a function in the/sessionrequest (feat(widget): userToken can be a function that returns a fresh token #476), so no other code changed.setUsercompares 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 ornullcounts 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(thesetUsersection) says it takes a function, what counts as the same visitor, and that the function reference has to be kept ifsetUseris called more than once.api/widget.api.mdis regenerated, and there is a patch changeset.Notes for review
setUsercall 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.How I Tested This
tests/user-token.test.ts: the function is called when a session is requested and not whensetUseris called; the same function again keeps the chat; a different function, a string,undefinedandnulleach clear it; a user with no token clears it; an anonymous visitor keeps the chat when signing in with a function;ChatCops.setUserin the npm and single-file builds; a later string replaces the function.tests/api.test.ts: the script tag loader keeps a function given tosetUseruntil the chat loads and passes it on unchanged, without calling it.tests/api.test-d.ts:setUsertakes a string or a function in both builds, and a function that returns a number is rejected.setUserin four ways (always clear, never clear, clear when anonymous, token not passed on); a test fails for each.dev),pnpm sizeis within the limits, andpnpm docs:checkfinds no errors in the widget docs. The whole e2e suite passes except the threeS7 reply with sourcestests, which fail on a cleandevhere 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.