Accumulating hundreds of anon-users using Realm GraphQL with an SPA

@MaBeuLux88 @Andrew_Morgan I’m currently using Realm GraphQL with an SPA and I’m experiencing the same issue for anonymous users as @Patrick_Mamba stated on Open API - anonymous user registered for each request :

I’ve tested with logged in user account and api keys and the issue doesn’t happen in those instances. It’s only anon-user that has the issue. Any idea on what can be the cause of this? While I’m dev/testing I have hundreds of anonymous users in the app.allUsers array:

Please advise, any suggestions/help would be greatly appreciated.

-Drew

Hi @ajedgarcraft,

Can you try to reproduce the python example I made in my last post in this topic and see if you can reproduce this behaviour? I wasn’t able to reproduce the problem last time so my guess is that it was a problem in the code rather than something from the platform.

Maybe try to add a log near the anon-user login method and one next to the GraphQL call and see if you are calling the login fonction only once and the GraphQL one multiple time, like in my python example?

Cheers,
Maxime.

1 Like

@MaBeuLux88 I checked again and it’s not happening on the requests, the anonymous user id is changing with every page reload. Is that normal?

Looks like an error in your code and you are calling the auth method on every refresh instead of storing the current user & reusing it.

1 Like

@MaBeuLux88 Thank you for clarifying. :fist_right: :fist_left:

Is there any documentation on how to handle that with realm-web@1.3.0?

When logging-in anonymous users as per: https://docs.mongodb.com/realm/authentication/anonymous/ I made the assumption that anonymous users were already handled:

“If the app does not explicitly log this anonymous user out, the same anonymous user is reused”

I’m using:

const credentials = Realm.Credentials.anonymous();

const user = await app.logIn(credentials);

I think I sorted it out. As @MaBeuLux88 ( :beers:) stated the current user needed to be referenced so I did something like this for my login function:

const anonymousLogin = async () => {
    const credentials = Realm.Credentials.anonymous();
    if (app.currentUser === null) {
      await app.logIn(credentials);

      return app.currentUser;
    }
    return app.currentUser;
  };

I was improperly referencing the user from the login function explained here: https://docs.mongodb.com/realm/web/authenticate/

It would be helpful for novices like myself if this was shown somewhere in the documentation. :grin:

2 Likes

Awesome, I’m glad you found the solution :smiley: !
It’s not easy to debug remotely without seeing the actual code :sweat_smile:.

To sum up, when you call app.logIn(creds), you are creating a new user each time if you are using the anon-auth.

The same logic would apply for any other auth system available - but you don’t want to re-authenticate the user each time. Especially if it’s just a refresh or the person browsing around your website. You want to re-use the same user object in that case.

Cheers,
Maxime.

1 Like

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