Class User


  • public class User
    extends Object
    A user holds the user's meta data and tokens for accessing Realm App functionality.

    The user is used to configure Synchronized Realms and gives access to calling Realm App Functions through Functions and accessing remote Realm App Mongo Databases through a MongoClient.

    See Also:
    App.login(Credentials), Builder(User, String)
    • Method Detail

      • getId

        public String getId()
        Returns the server id of the user.
        Returns:
        the server id of the user.
      • getProfile

        public UserProfile getProfile()
        Returns the profile for this user.
        Returns:
        the profile for this user
      • getIdentities

        public List<UserIdentity> getIdentities()
        Returns a new list of the user's identities.
        Returns:
        the list of identities.
        See Also:
        UserIdentity
      • getProviderType

        public Credentials.Provider getProviderType()
        Returns the provider type used to log the user
        Returns:
        the provider type of the user
      • getAccessToken

        public String getAccessToken()
        Returns the current access token for the user.
        Returns:
        the current access token.
      • getRefreshToken

        public String getRefreshToken()
        Returns the current refresh token for the user.
        Returns:
        the current refresh token.
      • getDeviceId

        public String getDeviceId()
        Returns a unique identifier for the device the user logged in to.
        Returns:
        a unique device identifier for the user.
      • getApp

        public App getApp()
        Returns the App this user is associated with.
        Returns:
        the App this user is associated with.
      • getCustomData

        public Document getCustomData()
        Return the custom user data associated with the user in the Realm App.

        The data is only refreshed when the user's access token is refreshed or when explicitly calling refreshCustomData().

        Returns:
        The custom user data associated with the user.
      • refreshCustomData

        public Document refreshCustomData()
        Re-fetch custom user data from the Realm App.
        Returns:
        The updated custom user data associated with the user.
        Throws:
        AppException - if the request failed in some way.
      • refreshCustomData

        public RealmAsyncTask refreshCustomData​(App.Callback<Document> callback)
        Re-fetch custom user data from the Realm App asynchronously.

        This is the asynchronous variant of refreshCustomData().

        Parameters:
        callback - The callback that will receive the result or any errors from the request.
        Returns:
        The task representing the ongoing operation.
        Throws:
        IllegalStateException - if not called on a looper thread.
      • isLoggedIn

        public boolean isLoggedIn()
        Returns true if the user is currently logged in. Returns whether or not this user is still logged into the MongoDB Realm App.
        Returns:
        true if still logged in, false if not.
      • linkCredentials

        public User linkCredentials​(Credentials credentials)
        Links the current user with a new user identity represented by the given credentials.

        Linking a user with more credentials, mean the user can login either of these credentials. It also makes it possible to "upgrade" an anonymous user by linking it with e.g. Email/Password credentials.

         
         // Example
         App app = new App("app-id")
         User user = app.login(Credentials.anonymous());
         user.linkCredentials(Credentials.emailPassword("email", "password"));
         
         

        Note: It is not possible to link two existing users of MongoDB Realm. The provided credentials must not have been used by another user.

        Parameters:
        credentials - the credentials to link with the current user.
        Returns:
        the User the credentials were linked to.
        Throws:
        IllegalStateException - if no user is currently logged in.
      • linkCredentialsAsync

        public RealmAsyncTask linkCredentialsAsync​(Credentials credentials,
                                                   App.Callback<User> callback)
        Links the current user with a new user identity represented by the given credentials.

        Linking a user with more credentials, mean the user can login either of these credentials. It also makes it possible to "upgrade" an anonymous user by linking it with e.g. Email/Password credentials.

         
         // Example
         App app = new App("app-id")
         User user = app.login(Credentials.anonymous());
         user.linkCredentials(Credentials.emailPassword("email", "password"));
         
         

        Note: It is not possible to link two existing users of MongoDB Realm. The provided credentials must not have been used by another user.

        Parameters:
        credentials - the credentials to link with the current user.
        callback - callback when user identities has been linked or it failed. The callback will always happen on the same thread as this method is called on.
        Throws:
        IllegalStateException - if called from a non-looper thread.
      • remove

        public User remove()
                    throws AppException
        Calling this will remove the user and any Realms the user has from the device. No data is removed from the server. If the user is logged in when calling this method, the user is logged out before any data is deleted.
        Returns:
        the user that was removed.
        Throws:
        AppException - if an error occurred while trying to remove the user.
      • removeAsync

        public RealmAsyncTask removeAsync​(App.Callback<User> callback)
        Calling this will asynchronously remove the user and any Realms the user has from the device. No data is removed from the server. If the user is logged in when calling this method, the user is logged out before any data is deleted.
        Parameters:
        callback - callback when removing the user has completed or failed. The callback will always happen on the same thread as this method is called on.
        Throws:
        IllegalStateException - if called from a non-looper thread.
      • logOut

        public void logOut()
                    throws AppException
        Log the user out of the Realm App. This will unregister them on the device and stop any synchronization to and from the users' Realms. Any Realms owned by the user will not be deleted from the device before remove() is called.

        Once the Realm App has confirmed the logout any registered AuthenticationListener will be notified and user credentials will be deleted from this device.

        Logging out anonymous users will remove them immediately instead of marking them as User.State.LOGGED_OUT.

        All other users will be marked as User.State.LOGGED_OUT and will still be returned by App.allUsers(). They can be removed completely by calling remove().

        Throws:
        AppException - if an error occurred while trying to log the user out of the Realm App.
      • logOutAsync

        public RealmAsyncTask logOutAsync​(App.Callback<User> callback)
        Log the user out of the Realm App asynchronously. This will unregister them on the device and stop any synchronization to and from the users' Realms. Any Realms owned by the user will not be deleted from the device before remove() is called.

        Once the Realm App has confirmed the logout any registered AuthenticationListener will be notified and user credentials will be deleted from this device.

        Logging out anonymous users will remove them immediately instead of marking them as User.State.LOGGED_OUT.

        All other users will be marked as User.State.LOGGED_OUT and will still be returned by App.allUsers(). They can be removed completely by calling remove().

        Parameters:
        callback - callback when logging out has completed or failed. The callback will always happen on the same thread as this method is called on.
        Throws:
        IllegalStateException - if called from a non-looper thread.
      • getApiKeys

        public ApiKeyAuth getApiKeys()
        Returns a wrapper for managing API keys controlled by the current user.
        Returns:
        wrapper for managing API keys controlled by the current user.
        Throws:
        IllegalStateException - if no user is currently logged in.
      • getFunctions

        public Functions getFunctions()
        Returns a functions manager for invoking MongoDB Realm Functions.

        This will use the associated app's default codec registry to encode and decode arguments and results.

        See Also:
        Functions
      • getFunctions

        public Functions getFunctions​(CodecRegistry codecRegistry)
        Returns a functions manager for invoking Realm Functions with custom codec registry for encoding and decoding arguments and results.
        Parameters:
        codecRegistry - The codec registry to use for encoding and decoding arguments and results towards the remote Realm App.
        See Also:
        Functions
      • getPush

        public Push getPush​(String serviceName)
        Returns the Push instance for managing push notification registrations.
        Parameters:
        serviceName - the service name used to connect to the server.
      • getMongoClient

        public MongoClient getMongoClient​(String serviceName)
        Returns a MongoClient instance for accessing documents in the database.
        Parameters:
        serviceName - the service name used to connect to the server.
      • equals

        public boolean equals​(@Nullable
                              Object o)
        Two Users are considered equal if they have the same user identity and are associated with the same app.
        Overrides:
        equals in class Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object