Skip to content

fix: reject an empty userId in setUserId instead of logging the user out - #101

Open
mattinannt wants to merge 2 commits into
mainfrom
fix/ENG-1499_empty-user-id
Open

mattinannt wants to merge 2 commits into
mainfrom
fix/ENG-1499_empty-user-id

Conversation

@mattinannt

Copy link
Copy Markdown
Member

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 returned Ok and did nothing.

setUserId now checks the id before it touches any state. An empty or whitespace-only id returns Err(MissingFieldError('userId')) with the message userId 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 setAttributes already uses for an unsupported value type: build the error, Logger.error, return Result.err.

Alternatives rejected

  • Log and return Ok, which is what the JS SDK does for an over-long id. The caller would get no signal, and Result already exists to carry this error.
  • A new InvalidUserIdError. FormbricksError is sealed, so a new subclass breaks any host code that switches on it exhaustively. MissingFieldError is documented as "missing or invalid", and setup() already uses it with a custom message.

Behaviour change for release notes: a host that called setUserId('') to log a user out now gets an Err and the user stays identified. It should call logout(). This is a two-way door, and the blast radius is limited to setUserId with a blank id. The public API shape does not change: same signature, and an existing error type.

Docs: dartdoc on user.setUserId and on Formbricks.setUserId, plus a line under the README method table.

Tests

Done when Test Red on main (cadc6a3)
A blank id with a user identified does not log them out test/user/user_test.dart › empty string when one set → MissingFieldError, user kept, nothing queued (also checks segments and the Embedded Data bag survive) yes, got Ok
A blank id from anonymous is rejected, nothing queued … › empty string from anonymous → MissingFieldError, nothing queued yes
A whitespace-only id is treated the same … › whitespace-only when one set → MissingFieldError, user kept yes

Commands run locally (Flutter 3.47.7 from .fvmrc):

  • make format-check: passes
  • make analyze-ci: No issues found!
  • make test: formbricks: SUCCESS, formbricks_playground: SUCCESS
  • make 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 e2e run. The golden journey asserts a non-empty E2E_USER_ID and expects -> ok, so it is unaffected. In the playground, an empty user-id field now shows setUserId('') -> missing_field: … instead of -> ok.

Open gaps

  • No test calls the public Formbricks.setUserId('') through the command queue. That path just returns the command's result, so the tests call user.setUserId directly, like the existing ones do.
  • Siblings in other repos, left for their own tickets: the JS SDK (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

…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
@mattinannt
mattinannt requested a review from Dhruwang December 6, 2026 06:18
@coderabbitai

coderabbitai Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

setUserId now returns MissingFieldError for empty or whitespace-only IDs before accessing user state. The supplied ID is trimmed only for validation. Documentation describes the error behavior and directs callers to logout() to clear the user. Tests check the error, preserved state, and absence of queued updates.

Merge Risk: Minimal · up to c2b59

Calling setUserId with an empty or whitespace-only ID now returns an error and leaves the current user unchanged. Callers who want to clear the user should use logout(). No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: setUserId with an empty userId now returns an error instead of logging the user out. It matches the changeset, which adds a blank-ID check in setUserId, error handlin…
Description check Passed The description explains the defect, the new behaviour, the rejected alternatives, the test table, the commands run, and the open gaps. It matches the changes in user.dart, formbricks_widget.dart, the…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: setUserId with an empty userId now returns an error instead of logging the user out. It matches the changeset, which adds a blank-ID check in setUserId, error handling, docs, and tests. The title is specific and concise.

Full details: Description check

Explanation

The description explains the defect, the new behaviour, the rejected alternatives, the test table, the commands run, and the open gaps. It matches the changes in user.dart, formbricks_widget.dart, the README, and user_test.dart.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: e852d7a0-33aa-4ff5-9607-b9957760d6d1
Commits

Reviewing files that changed from the base of the PR and between cadc6a3 and c2b5984.

Files selected for processing: 4

  • packages/formbricks/README.md
  • packages/formbricks/lib/src/user/user.dart
  • packages/formbricks/lib/src/widgets/formbricks_widget.dart
  • packages/formbricks/test/user/user_test.dart

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@sonarqubecloud

sonarqubecloud Bot commented Dec 6, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants