Class User
- java.lang.Object
-
- io.realm.mongodb.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 aMongoClient
.- See Also:
App.login(Credentials)
,Builder(User, String)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
User.State
The user's potential states.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
Two Users are considered equal if they have the same user identity and are associated with the same app.String
getAccessToken()
Returns the current access token for the user.ApiKeyAuth
getApiKeys()
Returns a wrapper for managing API keys controlled by the current user.App
getApp()
Returns theApp
this user is associated with.Document
getCustomData()
Return the custom user data associated with the user in the Realm App.String
getDeviceId()
Returns a unique identifier for the device the user logged in to.Functions
getFunctions()
Returns a functions manager for invoking MongoDB Realm Functions.Functions
getFunctions(CodecRegistry codecRegistry)
Returns a functions manager for invoking Realm Functions with custom codec registry for encoding and decoding arguments and results.String
getId()
Returns the server id of the user.List<UserIdentity>
getIdentities()
Returns a new list of the user's identities.MongoClient
getMongoClient(String serviceName)
Returns aMongoClient
instance for accessing documents in the database.UserProfile
getProfile()
Returns the profile for this user.Credentials.Provider
getProviderType()
Returns the provider type used to log the userPush
getPush(String serviceName)
Returns thePush
instance for managing push notification registrations.String
getRefreshToken()
Returns the current refresh token for the user.User.State
getState()
Returns theUser.State
the user is in.int
hashCode()
boolean
isLoggedIn()
Returns true if the user is currently logged in.User
linkCredentials(Credentials credentials)
Links the current user with a new user identity represented by the given credentials.RealmAsyncTask
linkCredentialsAsync(Credentials credentials, App.Callback<User> callback)
Links the current user with a new user identity represented by the given credentials.void
logOut()
Log the user out of the Realm App.RealmAsyncTask
logOutAsync(App.Callback<User> callback)
Log the user out of the Realm App asynchronously.Document
refreshCustomData()
Re-fetch custom user data from the Realm App.RealmAsyncTask
refreshCustomData(App.Callback<Document> callback)
Re-fetch custom user data from the Realm App asynchronously.User
remove()
Calling this will remove the user and any Realms the user has from the device.RealmAsyncTask
removeAsync(App.Callback<User> callback)
Calling this will asynchronously remove the user and any Realms the user has from the device.
-
-
-
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 theApp
this user is associated with.- Returns:
- the
App
this user is associated with.
-
getState
public User.State getState()
Returns theUser.State
the user is in.- Returns:
- the
User.State
of the user.
-
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 beforeremove()
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 byApp.allUsers()
. They can be removed completely by callingremove()
.- 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 beforeremove()
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 byApp.allUsers()
. They can be removed completely by callingremove()
.- 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 thePush
instance for managing push notification registrations.- Parameters:
serviceName
- the service name used to connect to the server.
-
getMongoClient
public MongoClient getMongoClient(String serviceName)
Returns aMongoClient
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.
-
-