Skip to content

Commit

Permalink
Merge pull request #1 from yendo-eng/prefer-cookies-to-local-storage-…
Browse files Browse the repository at this point in the history
…for-anonymous-id

Prefer ajs_anonymous_id from cookies to LocalStorage
  • Loading branch information
QoLTech authored Dec 17, 2023
2 parents d687327 + f73fe72 commit ab6f29b
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/core/lib/utils/store/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ class StoreImpl implements Store {

Future<Map<String, dynamic>?> _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<String, String>? data;
try {
data = localStorage.entries.firstWhere((i) => i.key == fileName);
Expand All @@ -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<String, dynamic>;

final jsonMap = json.decode(data.value) as Map<String, dynamic>;
if (anonymousId != null) {
jsonMap["anonymousId"] = anonymousId;
}

return jsonMap;
} else {
if (anonymousId != null) {
return {
"anonymousId": anonymousId,
};
}

return null;
}
}
Expand Down

0 comments on commit ab6f29b

Please sign in to comment.