Realm Sync broken after deleting user account

In my app, I use anonymous authentication to let user access public static data without having to create an account. I have set up a trigger to regularly delete anonymous users to cleanup the database. The anonymous users can also be deleted after 90 days, according to the doc.

The problem is that, after deleting an anonymous user account, when the user opens the app again, the following error occurs:

2021-02-01 18:24:32.305629+0100 App[3170:1683819] Sync: Connection[1]: Websocket: Expected HTTP response 101 Switching Protocols, but received:

HTTP/1.1 401 Unauthorized

cache-control: no-cache, no-store, must-revalidate

connection: close

content-length: 190

content-type: application/json

date: Mon, 01 Feb 2021 17:24:32 GMT

server: envoy

vary: Origin

x-envoy-max-retries: 0

x-frame-options: DENY

2021-02-01 18:24:32.305722+0100 App[3170:1683819] Sync: Connection[1]: Connection closed due to error

And on the server logs, an InvalidSession error is thrown. After this, the client can’t sync anymore to the server.

How can I “catch” this error and just make the user login anonymously again when that happens?

Are you able to check app.currentUser and check if it is not null. If it is, then try to log in another anonymous user again?

We have the same issue after we deleted a user in the App Users tab. If user have an Android device he can’t do anything the app always tries to use the same user ID and fails to connect. We tried all possible kind of data cleanup we can find and nothing seems to work.

Do you have an error handler on the client? See here:

And there should be an auth error here:

Hi Ian, thanks for your reply. We have an issue with Android devices. On iOS, after we reinstall the app new user ID is issued and a user can log in but on Android, the same ID is issued and a user can’t log in. Not sure how having an error handler could help are there any actions that can be performed during such kinds of errors on Android?

We first receive session error:

Connection[1]: Connect timeout
CLIENT_CONNECT_TIMEOUT(realm::sync::Client::Error:121): Sync connection was not fully established in time
Connection[1]: Resolving ‘ws.us-east-1.aws.realm.mongodb.com:443

And some time later

E/REALM_SYNC: Connection[1]: Websocket: Expected HTTP response 101 Switching Protocols, but received:
HTTP/1.1 401 Unauthorized
cache-control: no-cache, no-store, must-revalidate
connection: close
content-length: 190
content-type: application/json
date: Mon, 19 Apr 2021 08:03:21 GMT
server: envoy
vary: Origin
x-envoy-max-retries: 0
x-frame-options: DENY
I/REALM_SYNC: Connection[1]: Connection closed due to error

user?.logOut() worked. I received a new userID and was able to login successfully after that

Update: user?.logOut() worked only after app.removeUser(user) was called despite it says that the user should be removed during log out call.

Note: this issue is also tracked in this topic. I started the other topic for another reason, but it came down to the same problem.

To answer the questions:

@Sumedha_Mehta1 the user is not null, that is the problem. It is not null, but it is in an invalid state, and it can’t do anything without getting an invalidSession error. Sure, we could log in another user, but this particular user wouldn’t be deleted, neither would be the associated data, so it doesn’t look like a clean solution.

@Ian_Ward detecting the error is not a problem, a call to any Realm function will return an invalidSession so we know there is an error. The problem is, what to do after getting the error, and how to remove the user properly.