Class DittoAuthenticator
java.lang.Object
com.ditto.java.DittoAuthenticator
Handles authentication with Ditto Cloud for remote synchronization.
Use this class to log in to a remote authentication service when using an
OnlineWithAuthentication or Online identity.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandler for authentication expiration events.static interfaceCallback invoked when a login operation completes.static interfaceCallback invoked when a login operation completes with client info.static final recordResult of a login operation.static interfaceFunction to perform cleanup operations during logout.static interfaceListener for authentication status changes. -
Method Summary
Modifier and TypeMethodDescription@Nullable DittoAuthenticator.ExpirationHandlerReturns the current expiration handler, or null if none is set.@NonNull DittoAuthenticationStatusReturns the current authentication status.login(@NonNull String token, @NonNull DittoAuthenticationProvider provider) Logs in to Ditto with a third-party authentication token.login(@NonNull String token, @NonNull DittoAuthenticationProvider provider, @NonNull DittoAuthenticator.LoginCallbackWithClientInfoJSON callback) Logs in to Ditto with a third-party authentication token using a callback.Logs in to Ditto with a third-party authentication token.login(@NonNull String token, @NonNull String provider, @NonNull DittoAuthenticator.LoginCallbackWithClientInfoJSON callback) Logs in to Ditto with a third-party authentication token using a callback.voidlogout()Logs out of Ditto.voidlogout(@Nullable DittoAuthenticator.LogoutCleanup cleanup) Logs out of Ditto with optional cleanup.Returns a Flow Publisher that emits authentication status changes.observeStatus(@NonNull DittoAuthenticator.StatusListener listener) Observes authentication status changes using a listener.voidsetCallback(@NonNull DittoAuthenticationCallback callback) Deprecated.This method is deprecated.voidsetExpirationHandler(@Nullable DittoAuthenticator.ExpirationHandler expirationHandler) Sets a handler to be called when authentication is about to expire.
-
Method Details
-
setCallback
Deprecated.This method is deprecated. Use the following alternatives:
- For
onAuthenticationExpiringSoon: UsesetExpirationHandler(com.ditto.java.DittoAuthenticator.@org.checkerframework.checker.nullness.qual.Nullable ExpirationHandler) - For
onAuthenticationStatusDidChange: UseobserveStatus() - For
onAuthenticationRequired: CheckDittoAuthenticationStatusfromobserveStatus()
NOTE: legacy callback shouldn't be used together with
setExpirationHandler(com.ditto.java.DittoAuthenticator.@org.checkerframework.checker.nullness.qual.Nullable ExpirationHandler).This method will be removed in a future major version.
Sets the authentication callback to receive authentication events.- Parameters:
callback- theDittoAuthenticationCallbackto be notified of authentication events.
- For
-
getExpirationHandler
Returns the current expiration handler, or null if none is set.- Returns:
- the current
DittoAuthenticator.ExpirationHandler, or null.
-
setExpirationHandler
Sets a handler to be called when authentication is about to expire.
This handler is called when authentication is about to expire and allows you to perform login or other necessary actions before expiration.
Note: This handler must be set when using Server-based connectivity (
DittoConfig.Connect.Server), orwill throw ainvalid reference
Ditto.startSyncDittoException.AuthenticationExceptionwith reason.invalid reference
ExpirationHandlerMissing- Parameters:
expirationHandler- theDittoAuthenticator.ExpirationHandlerto handle expiring authentication, or null to clear.
-
getStatus
Returns the current authentication status.- Returns:
- the current
DittoAuthenticationStatus.
-
login
public @NonNull CompletionStage<DittoAuthenticator.LoginResult> login(@NonNull String token, @NonNull DittoAuthenticationProvider provider) Logs in to Ditto with a third-party authentication token. The returnedCompletionStagewill complete once the login attempt finishes. TheDittoAuthenticator.LoginResultcontains optional client info JSON from the auth webhook and an error if the login failed.- Parameters:
token- the authentication token required to log in.provider- theDittoAuthenticationProvider(development or custom).- Returns:
- a
CompletionStagethat completes with theDittoAuthenticator.LoginResult.
-
login
public @NonNull CompletionStage<DittoAuthenticator.LoginResult> login(@NonNull String token, @NonNull String provider) Logs in to Ditto with a third-party authentication token. The returnedCompletionStagewill complete once the login attempt finishes. TheDittoAuthenticator.LoginResultcontains optional client info JSON from the auth webhook and an error if the login failed.- Parameters:
token- the authentication token required to log in.provider- the name of the authentication provider.- Returns:
- a
CompletionStagethat completes with theDittoAuthenticator.LoginResult.
-
login
public DittoAsyncCancellable login(@NonNull String token, @NonNull String provider, @NonNull DittoAuthenticator.LoginCallbackWithClientInfoJSON callback) Logs in to Ditto with a third-party authentication token using a callback.- Parameters:
token- the authentication token required to log in.provider- the name of the authentication provider.callback- theDittoAuthenticator.LoginCallbackWithClientInfoJSONto be called when login completes.- Returns:
- a
DittoAsyncCancellablethat can be used to cancel the login attempt.
-
login
public DittoAsyncCancellable login(@NonNull String token, @NonNull DittoAuthenticationProvider provider, @NonNull DittoAuthenticator.LoginCallbackWithClientInfoJSON callback) Logs in to Ditto with a third-party authentication token using a callback.- Parameters:
token- the authentication token required to log in.provider- theDittoAuthenticationProvider(development or custom).callback- theDittoAuthenticator.LoginCallbackWithClientInfoJSONto be called when login completes.- Returns:
- a
DittoAsyncCancellablethat can be used to cancel the login attempt.
-
logout
public void logout()Logs out of Ditto. This will stop sync, shut down all replication sessions, and remove any cached authentication credentials. Note that this does not remove any data from the store. -
logout
Logs out of Ditto with optional cleanup. This will stop sync, shut down all replication sessions, and remove any cached authentication credentials. The optional cleanup function allows you to perform any required cleanup of the store as part of the logout process.- Parameters:
cleanup- an optionalDittoAuthenticator.LogoutCleanupfunction to perform cleanup operations.
-
observeStatus
Returns a Flow Publisher that emits authentication status changes.- Returns:
- a
Flow.Publisherthat emitsDittoAuthenticationStatusupdates.
-
observeStatus
Observes authentication status changes using a listener.- Parameters:
listener- theDittoAuthenticator.StatusListenerto be notified of status changes.- Returns:
- an
AutoCloseablethat can be used to stop observing.
-