diff --git a/packages/formbricks/README.md b/packages/formbricks/README.md index 3ed5cb2..0b1f3ec 100644 --- a/packages/formbricks/README.md +++ b/packages/formbricks/README.md @@ -85,6 +85,9 @@ Each returns a `Future>`. | `Formbricks.setAppearance(FormbricksAppearance)` | Surveys render `light` (default), `dark`, or `system`. See Dark mode. | | `Formbricks.logout()` | Reset to an anonymous session. | +`setUserId('')` (or a whitespace-only id) returns an error and changes nothing, +so an identified user stays identified. Call `logout()` to clear the user. + Attribute keys must be lowercase letters, numbers, and underscores, and start with a letter. `DateTime` values are sent as UTC ISO-8601 strings; numbers stay numbers. Identity/attribute changes are debounced (~500 ms) and coalesced into a diff --git a/packages/formbricks/lib/src/user/user.dart b/packages/formbricks/lib/src/user/user.dart index 903ad33..46e1a51 100644 --- a/packages/formbricks/lib/src/user/user.dart +++ b/packages/formbricks/lib/src/user/user.dart @@ -16,12 +16,15 @@ import 'update_queue.dart'; /// Identifies the current contact as [userId]. /// +/// - Empty or whitespace-only [userId] → `Err(MissingFieldError)` and nothing +/// changes, so an identified user stays identified. Use [logout] to clear +/// the user. /// - Same value already set → idempotent `Ok` no-op. /// - A *different* userId already set → [tearDown] the previous user state /// first, then queue the new id. /// - Anonymous → queue the id directly (no teardown). /// -/// Returns immediately with `Ok`; the backend `createOrUpdateUser` fires from +/// Otherwise returns immediately with `Ok`; the backend `createOrUpdateUser` fires from /// the debounced [UpdateQueue]. Future> setUserId( String userId, { @@ -31,6 +34,18 @@ Future> setUserId( final cfg = config ?? FormbricksConfig.instance; final q = queue ?? UpdateQueue.instance; + // Validate before touching any state: a blank id is not an identity, and + // treating it as a switch would tear down (log out) the current user and + // then identify no one. + if (userId.trim().isEmpty) { + final error = MissingFieldError( + 'userId', + message: 'userId must not be empty. Call logout() to clear the user.', + ); + Logger.error(error.message); + return Result.err(error); + } + final currentUserId = cfg.get().user.data.userId; if (currentUserId == userId) { diff --git a/packages/formbricks/lib/src/widgets/formbricks_widget.dart b/packages/formbricks/lib/src/widgets/formbricks_widget.dart index bcf298a..0d8c4aa 100644 --- a/packages/formbricks/lib/src/widgets/formbricks_widget.dart +++ b/packages/formbricks/lib/src/widgets/formbricks_widget.dart @@ -148,8 +148,10 @@ class Formbricks extends StatefulWidget { /// Identifies the current contact as [userId] (`checkSetup: true`). /// /// Idempotent for the already-set value; switching to a different id resets - /// the prior user state first. The backend sync runs from the debounced - /// update queue, so this resolves before the network call completes. + /// the prior user state first. An empty or whitespace-only [userId] returns + /// `Err(MissingFieldError)` and changes nothing; use [logout] to clear the + /// user. The backend sync runs from the debounced update queue, so this + /// resolves before the network call completes. static Future> setUserId(String userId) { return _queue.add>( () => user.setUserId(userId), diff --git a/packages/formbricks/test/user/user_test.dart b/packages/formbricks/test/user/user_test.dart index ae78d5a..696821a 100644 --- a/packages/formbricks/test/user/user_test.dart +++ b/packages/formbricks/test/user/user_test.dart @@ -4,7 +4,9 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:formbricks/src/common/api_client.dart'; import 'package:formbricks/src/common/config.dart'; import 'package:formbricks/src/common/logger.dart'; +import 'package:formbricks/src/common/result.dart'; import 'package:formbricks/src/survey/embedded_data.dart'; +import 'package:formbricks/src/types/errors.dart'; import 'package:formbricks/src/user/update_queue.dart'; import 'package:formbricks/src/user/user.dart'; import 'package:http/http.dart' as http; @@ -122,6 +124,74 @@ void main() { expect(result.isOk, isTrue); expect(queue.pendingUserId, 'u1'); }); + + test( + 'empty string when one set → MissingFieldError, user kept, nothing queued', + () async { + final config = await _seed(userId: 'u1'); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + EmbeddedDataStore.instance + ..clear() + ..set({'plan': 'pro'}); + addTearDown(EmbeddedDataStore.instance.clear); + + final result = await setUserId('', config: config, queue: queue); + + expect(result, isA>()); + final error = (result as Err).error; + expect(error, isA()); + expect((error as MissingFieldError).field, 'userId'); + // No teardown: the identified user, their segments and the Embedded + // Data bag all survive. + expect(config.get().user.data.userId, 'u1'); + expect(config.get().user.data.segments, ['seg_1']); + expect(EmbeddedDataStore.instance.snapshot(), {'plan': 'pro'}); + expect(queue.isEmpty, isTrue); + }, + ); + + test( + 'empty string from anonymous → MissingFieldError, nothing queued', + () async { + final config = await _seed(); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + + final result = await setUserId('', config: config, queue: queue); + + expect(result, isA>()); + expect( + (result as Err).error, + isA(), + ); + expect(config.get().user.data.userId, isNull); + expect(queue.isEmpty, isTrue); + }, + ); + + test( + 'whitespace-only when one set → MissingFieldError, user kept', + () async { + final config = await _seed(userId: 'u1'); + final queue = UpdateQueue.instance + ..configOverride = config + ..apiClientOverride = _noopApi(); + + final result = await setUserId(' ', config: config, queue: queue); + + expect(result, isA>()); + expect( + (result as Err).error, + isA(), + ); + expect(config.get().user.data.userId, 'u1'); + expect(config.get().user.data.segments, ['seg_1']); + expect(queue.isEmpty, isTrue); + }, + ); }); group('logout', () {