Class Credentials


  • public class Credentials
    extends Object
    Credentials represent a login with a given login provider, and are used by the MongoDB Realm to verify the user and grant access. The Credentials.Provider.EMAIL_PASSWORD provider is enabled by default. All other providers must be enabled on MongoDB Realm to work.

    Note that users wanting to login using Email/Password must register first using EmailPasswordAuth.registerUser(String, String).

    Credentials are used the following way:
     
     // Example
     App app = new App("app-id");
     Credentials credentials = Credentials.emailPassword("email", "password");
     User user = app.loginAsync(credentials, new App.Callback<User>() {
       \@Override
       public void onResult(Result<User> result) {
         if (result.isSuccess() {
           handleLogin(result.get());
         } else {
           handleError(result.getError());
         }
       }
     ));
     }
     
     
    See Also:
    Authentication Providers
    • Method Detail

      • anonymous

        public static Credentials anonymous()
        Creates credentials representing an anonymous user.

        Logging the user out again means that data is lost with no means of recovery and it isn't possible to share the user details across devices.

        The anonymous user must be linked to another real user to preserve data after a log out.

        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • apiKey

        public static Credentials apiKey​(String key)
        Creates credentials representing a login using a user API key.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        key - the API key to use for login.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • apple

        public static Credentials apple​(String idToken)
        Creates credentials representing a login using an Apple ID token.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        idToken - the ID token generated when using your Apple login.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • customFunction

        public static Credentials customFunction​(Document arguments)
        Creates credentials representing a remote function from MongoDB Realm using a Document which will be parsed as an argument to the remote function, so the keys must match the format and names the function expects.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        arguments - document containing the function arguments.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • emailPassword

        public static Credentials emailPassword​(String email,
                                                String password)
        Creates credentials representing a login using email and password.
        Parameters:
        email - email of the user logging in.
        password - password of the user logging in.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • facebook

        public static Credentials facebook​(String accessToken)
        Creates credentials representing a login using a Facebook access token.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        accessToken - the access token returned when logging in to Facebook.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • google

        public static Credentials google​(String token,
                                         GoogleAuthType type)
        Creates credentials representing a login using a Google access token of a given GoogleAuthType.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        token - the access token returned when logging in to Google.
        type - the access token type
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • jwt

        public static Credentials jwt​(String jwtToken)
        Creates credentials representing a login using a JWT Token. This token is normally generated after a custom OAuth2 login flow.

        This provider must be enabled on MongoDB Realm to work.

        Parameters:
        jwtToken - the jwt token returned after a custom login to a another service.
        Returns:
        a set of credentials that can be used to log into MongoDB Realm using App.loginAsync(Credentials, App.Callback).
      • getIdentityProvider

        public Credentials.Provider getIdentityProvider()
        Returns the identity provider used to authenticate with.
        Returns:
        the provider identifying the chosen credentials.
      • asJson

        public String asJson()
        Returns the credentials object serialized as a json string.
        Returns:
        a json serialized string of the credentials object.