pub struct DittoAuthenticator { /* private fields */ }Expand description
Use ditto.auth() to manage authentication when using applicable identites.
The DittoAuthenticator is available when using the OnlinePlayground
and OnlineWithAuthentication identities.
Implementations§
Source§impl DittoAuthenticator
impl DittoAuthenticator
Sourcepub fn set_expiration_handler<F>(&self, handler: F)where
F: DittoAuthExpirationHandler,
pub fn set_expiration_handler<F>(&self, handler: F)where
F: DittoAuthExpirationHandler,
Set a callback to notify the client when the authentication is expiring.
When using DittoConfigConnect::Server { .. } mode, Ditto requires you to register an
“expiration handler” for authentication. This handler is called for an initial
authentication and periodically thereafter when the current authentication is near to
expiration.
For more details about authentication, see the Ditto Auth and Authorization docs.
For more details about the expiration handler, see the DittoAuthExpirationHandler trait.
§Example
async fn sample_get_token() -> anyhow::Result<String> {
// e.g. reqwest::get("https://example.com/token").await?.text().await?;
Ok("token".to_string())
}
let auth = ditto
.auth()
.expect("Auth is available in Server connect mode");
auth.set_expiration_handler(async |ditto: &Ditto, duration_remaining| {
let auth = ditto
.auth()
.expect("Auth is available in Server connect mode");
// Call your auth service to get a new token
let token = sample_get_token().await.unwrap();
// Where "my-provider" is the name of the Authentication Webhook
// you've configured on the Ditto Portal
let result = auth.login(&token, "my-provider");
if let Err(login_error) = result {
// Handle login error, e.g.:
error!("Failed to login: {}", login_error);
}
});Sourcepub fn clear_expiration_handler(&self)
pub fn clear_expiration_handler(&self)
Clear the expiration handler, if set.
Source§impl DittoAuthenticator
impl DittoAuthenticator
Sourcepub fn login(
&self,
token: &str,
provider: &str,
) -> Result<AuthenticationClientFeedback, DittoError>
pub fn login( &self, token: &str, provider: &str, ) -> Result<AuthenticationClientFeedback, DittoError>
Asks the Ditto instance to make an auth request to the configured
[Identity]’s auth URL with a single token parameter.
token: An auth or API token you have configured.provider: The name of an authentication provider web hook you have configured in Ditto. Cloud, which will accept thetokenand contact your Auth service
Sourcepub fn logout<R>(
&self,
cleanup: impl FnOnce(Ditto) -> R,
) -> Result<R, DittoError>
pub fn logout<R>( &self, cleanup: impl FnOnce(Ditto) -> R, ) -> Result<R, DittoError>
Log out of Ditto.
Shutdown all replication sessions and remove any cached authentication credentials. This does not remove the local data store.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Query whether Ditto has a valid authentication token.
This will only be true when using an
OnlineWithAuthentication identity, after a
successful login. If the authentication token is allowed to expire then it will return
false instead.
Sourcepub fn user_id(&self) -> Option<String>
pub fn user_id(&self) -> Option<String>
The currently logged-in user ID.
This will return None if there is no valid authentication or an
OnlineWithAuthentication identity is not being
used.
Trait Implementations§
Source§impl Clone for DittoAuthenticator
impl Clone for DittoAuthenticator
Source§fn clone(&self) -> DittoAuthenticator
fn clone(&self) -> DittoAuthenticator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more