Introspecting the user's role on the client

Just started experimenting with MongoDB Realm using the iOS Swift SDK. If I want to present a different UI based on the user’s role(s), is there a built in mechanism to introspect the user roles using the SDK, or do I need to define custom user data to achieve that?

Using custom user data that is used for defining roles and the UI is probably the way to go here.

Trying to do that but the custom user data on the client remains nil.

This is what I did:

  • Enabled custom user data in the Realm UI and specified the cluster, db, collection, and User ID field.
  • Manually created a document in the custom collection and added the user id field with the ObjectID of the user along with another custom field (and of course the _id of the custom object).
  • One the client, I’m trying to use app.currentUser().customData - but it’s nil.

What am I missing? Also, is there a tutorial on using custom user data in the iOS SDK?

Hi Nimi,

Can you paste the document you added? The object_id will need to be in string format.

On top of that, it would also help to logout of your application and log back in to the application for that user to see the changes.

{"_id":{"$oid":“5f16ec54edf52a664a47709b”},“userID”:{"$oid":“5f144bbdf6355fb1cb482543”},“roles”:{"$numberInt":“1”}}

The collection for the custom data is called User. Is that a valid name or can it cause some kind of collision with the user object?

I did not include a _partitionKey in the custom data. Is that required? I’m assuming not as this object supposedly lives outside of any realm, right?

Logging out and back in did not resolve the issue.

the userID will have to be a string as I mentioned in the previous comment. Can you try:

{"_id":{"$oid":“5f16ec54edf52a664a47709b”},“userID”: “5f144bbdf6355fb1cb482543”,“roles”:{"$numberInt":“1”}}

It works! Sorry, I rushed through your suggestion to turn it into a string and just looked at the value itself, not the ObjectID wrapper…

Thanks so much for your prompt and useful responses!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.