How to use custom user data in custom realm flex sync permissions

Hello! I hope you can help me and thanks in advance! I’ve got Realm Flex sync set up for my mobile app. I’d like the app services to only let the devices download certain data from my db, data which contains a value equals to a value in the custom user data object (i.e. only data belonging to that user). However, I can’t get the Custom User Data setup to work properly, and/or can’t reference the custom user data properly in the realm sync permissions, even with the given examples. The sync permissions work fine when using a constant, so it’s really referencing the custom user data where I have issues… I’ve attached some screenshots highlighting my setup with realm flex sync and custom user data here:




Hi @Eric_Klaesson,

The correct syntax would be wasteOperator: "%%user.custom_data.wasteOperator" (note the nested custom_data field). See https://www.mongodb.com/docs/atlas/app-services/sync/configure/permissions/ for additional details.

Hope this helps!

Hi Kiro! Thanks for your response!:slight_smile: I did try this already, but the result is the same. On the error (“BadValue”) it sounds like the custom user data is simply not there. In the %%user variable, who provides this? Is it from within the app trying to sync? When I checked if the device had the custom user data (io.realm.mongodb.User.getCustomData()), the returned Document was empty

The error is indicating that your expansion is evaluating to undefined so the resolved expression is malformed. Looking at your screenshots, it seems like the issue is that your linked “User ID Field” is of type ObjectID, while the ID on user objects is actually a string. Can you try updating that and see if that starts working?

See https://www.mongodb.com/docs/atlas/app-services/users/enable-custom-user-data/#specify-the-user-id-field for more details.

Ah okay, that is one mistake corrected. I now use the field “username” for the User ID Field in the Custom User Data settings. I reinstalled the app on the device to start afresh. However, the error is still the exact same, unfortunately.

I’m still not sure how Atlas App Services picks the correct custom data user object and uses if for the sync permissions. And maybe therein lies the problem? When the device tries to sync data, it has to somehow provide a username or some link to its custom data user object, no?

Hi @Eric_Klaesson, I think this section in the docs might help answer your question.

For sync specifically, the %%user.custom_data expansion used in permissions will be populated with the custom user document for the user passed to the realm config.

Let me know if it’s still unclear!