Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/formbricks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ Each returns a `Future<Result<void, FormbricksError>>`.
| `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
Expand Down
17 changes: 16 additions & 1 deletion packages/formbricks/lib/src/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<void, FormbricksError>> setUserId(
String userId, {
Expand All @@ -31,6 +34,18 @@ Future<Result<void, FormbricksError>> 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) {
Expand Down
6 changes: 4 additions & 2 deletions packages/formbricks/lib/src/widgets/formbricks_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<void, FormbricksError>> setUserId(String userId) {
return _queue.add<Result<void, FormbricksError>>(
() => user.setUserId(userId),
Expand Down
70 changes: 70 additions & 0 deletions packages/formbricks/test/user/user_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Err<void, FormbricksError>>());
final error = (result as Err<void, FormbricksError>).error;
expect(error, isA<MissingFieldError>());
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<Err<void, FormbricksError>>());
expect(
(result as Err<void, FormbricksError>).error,
isA<MissingFieldError>(),
);
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<Err<void, FormbricksError>>());
expect(
(result as Err<void, FormbricksError>).error,
isA<MissingFieldError>(),
);
expect(config.get().user.data.userId, 'u1');
expect(config.get().user.data.segments, ['seg_1']);
expect(queue.isEmpty, isTrue);
},
);
});

group('logout', () {
Expand Down
Loading