Anonymous Login with React Native returning null user

Hello,

I am trying to allow anonymous login for my React Native app.
I have the loginAnonymous function implemented in my AuthProvider:

async function loginAnonymous() {
      // Create an anonymous credential
      const credentials = Realm.Credentials.anonymous();
      try {
        // Authenticate the user
        const user = await app.logIn(credentials);
        // `App.currentUser` updates to match the logged in user
        assert(user.id === app.currentUser.id)
        return user
      } catch(err) {
        console.error("Failed to log in", err);
      }
    }

When I call it, I can see that it is successful in the mongodb realm backend, however, it returns a null user/credentials.

From my reading, I assumed that a unique ID would be returned.
“Anonymous user objects have a unique ID value but no other metadata fields or configuration options.”

However, when I was reading into the Realm library, I noticed that the credentials payload for anonymous returns an empty object {}.

declare namespace Realm {
    namespace Credentials {
        /**
         * Payload sent when authenticating using the [Anonymous Provider](https://docs.mongodb.com/realm/authentication/anonymous/).
         */
        type AnonymousPayload = {};

        /**
         * Payload sent when authenticating using the [Email/Password Provider](https://docs.mongodb.com/realm/authentication/email-password/).
         */
        type EmailPasswordPayload = {
            /**
             * The end-users username.
             * Note: This currently has to be an email.
             */
            username: string;

            /**
             * The end-users password.
             */
            password: string;
        };

I was expecting some sort of ID being returned as the user. My app currently looking at the user object to dictate wheter or not they see the login screen.

Can anyone assist?

Thanks

Hi there - welcome to the forum :slight_smile:

What is null, the credentials or the user?

The AnonymousPayload that you’re referring to is the data sent to the server when authenticating, which is the empty object when authenticating anonymously. The server will create a user and respond to the auth request with a user object, including its unique id created by the server.

If you think there is infact a bug in the Realm JS SDK, you might consider creating an issue on GitHub.