From f73fe729c25333cacdbf03baf56f35d2b4f486ee Mon Sep 17 00:00:00 2001 From: Trevor Williams Date: Sun, 17 Dec 2023 14:56:07 -0600 Subject: [PATCH] Prefer ajs_anonymous_id from cookies to LocalStorage --- packages/core/lib/utils/store/web.dart | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/utils/store/web.dart b/packages/core/lib/utils/store/web.dart index a7cb8c1..09ddffe 100644 --- a/packages/core/lib/utils/store/web.dart +++ b/packages/core/lib/utils/store/web.dart @@ -35,6 +35,20 @@ class StoreImpl implements Store { Future?> _readFromStorage(String fileKey) async { final fileName = _getFileName(fileKey); + + String? anonymousId; + if (fileKey == "userInfo") { + final cookies = html.document.cookie?.split(";"); + if (cookies != null && cookies.isNotEmpty) { + for (var cookie in cookies) { + final cookieParts = cookie.split("="); + if (cookieParts[0].trim() == "ajs_anonymous_id") { + anonymousId = cookieParts[1]; + } + } + } + } + MapEntry? data; try { data = localStorage.entries.firstWhere((i) => i.key == fileName); @@ -43,10 +57,28 @@ class StoreImpl implements Store { } if (data != null) { if (data.value == "{}") { + if (anonymousId != null) { + return { + "anonymousId": anonymousId, + }; + } + return null; // Prefer null to empty map, because we'll want to initialise a valid empty value. } - return json.decode(data.value) as Map; + + final jsonMap = json.decode(data.value) as Map; + if (anonymousId != null) { + jsonMap["anonymousId"] = anonymousId; + } + + return jsonMap; } else { + if (anonymousId != null) { + return { + "anonymousId": anonymousId, + }; + } + return null; } }