Sources of Truths - nazrinharris/pecunia GitHub Wiki
I'd like to keep track of where I keep data as sources as truths, as these may need to be updated when I change a certain part of data that is spread to multiple sources of truths.
User Profile Data (PecuniaUser)
Remote User - Supabase Auth Database (check implementation)
For this, the user is simply stored within the User of Supabase. It's pretty dead simple. And currently, it stores like this:
// Retrieved from AuthRemoteDSHelper - mapSupaUserToDTO()
PecuniaUserDTO(
uid: user.id,
email: user.email,
username: user.userMetadata!['username'] as String,
dateCreated: DateTime.parse(user.createdAt),
)
where user is the User object retrieved from Supabase. This is the only source of truth for a remote user.
Local User - Flutter Secure Storage
For this, it's a quite different as its stored in the secure storage. The structure is as follows:
// Retrieved from AuthSecuredStorageManager - storeUserCredentials()
await flutterSecureStorage.write(key: kPecuniaUserKey(args.user.uid), value: jsonEncode(args.user.toJson()));
await flutterSecureStorage.write(key: kPecuniaUserHashedPasswordKey(args.user.uid), value: args.hashedPassword);
await flutterSecureStorage.write(key: kPecuniaUserUidKey(args.user.email!), value: args.user.uid);
As you can see, the entirety of the PecuniaUser object is stored in kPecuniaUserKey. This means that any change of PecuniaUser merely needs to update kPecuniaUserKey. This is the only source of truth for a local user.
Saved Users (both local and remote) - Shared Preferences
Since saved users (stored in kPrefsSavedUsers) are merely for records (as in it technically doesn't affect app functionality), this isn't a critical storage. It is not a source of truth. Though this list is still useful. The structure is as follows:
//Retrieved from AuthLocalDS - storeSavedUser()
final allUsersString = prefs.getString(kPrefsSavedUsers) ?? '{}';
final allUsers = jsonDecode(allUsersString) as Map<String, dynamic>;
allUsers[user.uid] = user.toJson();
await prefs.setString(kPrefsSavedUsers, jsonEncode(allUsers));
As you can see, the entirety of PecuniaUser is stored to the uid of the user in the allUsers map. So updating that is also simple.