Repository navigation
fix: reject an empty userId in setUserId instead of logging the user out - #101
mattinannt wants to merge 2 commits into
Conversation
…out [ENG-1499]
setUserId('') treated the empty string as a switch to a different user: it
tore down the identified user's state, cleared the Embedded Data bag, and
then identified no one. It now returns Err(MissingFieldError('userId')) and
changes nothing.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011A96cvjgJLErFw37fziM7Z
…ocs [ENG-1499] Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011A96cvjgJLErFw37fziM7Z
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|



What & why
Formbricks.setUserId('')treated the empty string as a switch to a different user. With a user already identified, it tore down that user's state (user id, segments, cached surveys), cleared the Embedded Data bag, then queued''. The update queue never sends a blank id, so the SDK quietly logged the user out and identified no one. From anonymous, it returnedOkand did nothing.setUserIdnow checks the id before it touches any state. An empty or whitespace-only id returnsErr(MissingFieldError('userId'))with the messageuserId must not be empty. Call logout() to clear the user., logs that error, and changes nothing. The identified user stays identified. A whitespace-only id is rejected too: before,' 'logged out the current user and then identified a contact named' 'with the backend.This follows the pattern
setAttributesalready uses for an unsupported value type: build the error,Logger.error, returnResult.err.Alternatives rejected
Ok, which is what the JS SDK does for an over-long id. The caller would get no signal, andResultalready exists to carry this error.InvalidUserIdError.FormbricksErrorissealed, so a new subclass breaks any host code that switches on it exhaustively.MissingFieldErroris documented as "missing or invalid", andsetup()already uses it with a custom message.Behaviour change for release notes: a host that called
setUserId('')to log a user out now gets anErrand the user stays identified. It should calllogout(). This is a two-way door, and the blast radius is limited tosetUserIdwith a blank id. The public API shape does not change: same signature, and an existing error type.Docs: dartdoc on
user.setUserIdand onFormbricks.setUserId, plus a line under the README method table.Tests
main(cadc6a3)test/user/user_test.dart›empty string when one set → MissingFieldError, user kept, nothing queued(also checks segments and the Embedded Data bag survive)Ok… › empty string from anonymous → MissingFieldError, nothing queued… › whitespace-only when one set → MissingFieldError, user keptCommands run locally (Flutter 3.47.7 from
.fvmrc):make format-check: passesmake analyze-ci:No issues found!make test:formbricks: SUCCESS,formbricks_playground: SUCCESSmake pub-publish-dry-run:0 warnings(only the hint about the dev version number)End to end: a container has no device or emulator, so the evidence is the unit tests above plus the PR's
e2erun. The golden journey asserts a non-emptyE2E_USER_IDand expects-> ok, so it is unaffected. In the playground, an empty user-id field now showssetUserId('') -> missing_field: …instead of-> ok.Open gaps
Formbricks.setUserId('')through the command queue. That path just returns the command's result, so the tests calluser.setUserIddirectly, like the existing ones do.formbricks/formbricks,packages/js-core/src/lib/user/user.ts) has the same defect. An empty id passes its length check, tears down the current user and identifies no one. The other SDKs were not checked.Fixes ENG-1499
Generated by Claude Code