[MongoDB Realm] What to do after `invalidSession` error?

In my app, users have the option to delete their account. This will call a Realm function that deletes the user account with all associated data. The problem is, after calling this function, any Realm related call will result in an invalidSession error, which is normal since the local cached user does not exist anymore — I need to log out the user.

I tried calling currentUser.logOut() and currentUser.remove() right after the call to the delete function, but both result in an invalidSession error. So, how can I “reset” the session and remove everything related to the old user, to that the app is in a new state, ready for another login?

You’ll want to call removeUser before calling the function to delete the user however you do need a valid user to call a function - so what you could do is call a Webhook which triggers the function to clean up the user on the backend Realm Cloud.

@Jean-Baptiste_Beau What SDK are you using?

@Ian_Ward I rely on app authentication to make sure users can only delete their own account, for obvious security reasons. Is it possible to have this kind of security using a webhook, even if the user is not connected?

@kraenhansen I’m using the iOS SDK, latest version.