use crate::api;
use crate::ApiKey;
use crate::Client;
use crate::Email;
use crate::ExpiresIn;
use crate::IdToken;
use crate::IdpPostBody;
use crate::LanguageCode;
use crate::OAuthContinueUri;
use crate::OAuthRequestUri;
use crate::Password;
use crate::ProviderId;
use crate::RefreshToken;
use crate::Result;
use crate::Session;
#[derive(Clone, Debug)]
pub struct Config {
api_key: ApiKey,
client: Client,
}
impl Config {
pub fn new(api_key: ApiKey) -> Self {
Self {
api_key,
client: Client::new(),
}
}
#[cfg(feature = "custom_client")]
pub fn custom(
api_key: ApiKey,
client: Client,
) -> Self {
Self {
api_key,
client,
}
}
pub async fn sign_up_with_email_password(
&self,
email: Email,
password: Password,
) -> Result<Session> {
let request_payload =
api::SignUpWithEmailPasswordRequestBodyPayload::new(
email.inner().to_string(),
password.inner().to_string(),
);
let response_payload = api::sign_up_with_email_password(
&self.client,
&self.api_key,
request_payload,
)
.await?;
Ok(Session {
client: self.client.clone(),
api_key: self.api_key.clone(),
id_token: IdToken::new(response_payload.id_token),
expires_in: ExpiresIn::parse(response_payload.expires_in)?,
refresh_token: RefreshToken::new(response_payload.refresh_token),
})
}
pub async fn sign_in_with_email_password(
&self,
email: Email,
password: Password,
) -> Result<Session> {
let request_payload =
api::SignInWithEmailPasswordRequestBodyPayload::new(
email.inner().to_string(),
password.inner().to_string(),
);
let response_payload = api::sign_in_with_email_password(
&self.client,
&self.api_key,
request_payload,
)
.await?;
Ok(Session {
client: self.client.clone(),
api_key: self.api_key.clone(),
id_token: IdToken::new(response_payload.id_token),
expires_in: ExpiresIn::parse(response_payload.expires_in)?,
refresh_token: RefreshToken::new(response_payload.refresh_token),
})
}
pub async fn sign_in_anonymously(&self) -> Result<Session> {
let request_payload = api::SignInAnonymouslyRequestBodyPayload::new();
let response_payload = api::sign_in_anonymously(
&self.client,
&self.api_key,
request_payload,
)
.await?;
Ok(Session {
client: self.client.clone(),
api_key: self.api_key.clone(),
id_token: IdToken::new(response_payload.id_token),
expires_in: ExpiresIn::parse(response_payload.expires_in)?,
refresh_token: RefreshToken::new(response_payload.refresh_token),
})
}
pub async fn sign_in_with_oauth_credential(
&self,
request_uri: OAuthRequestUri,
post_body: IdpPostBody,
) -> Result<Session> {
let request_payload =
api::SignInWithOAuthCredentialRequestBodyPayload::new(
request_uri
.inner()
.to_string(),
post_body,
false,
);
let response_payload = api::sign_in_with_oauth_credential(
&self.client,
&self.api_key,
request_payload,
)
.await?;
Ok(Session {
client: self.client.clone(),
api_key: self.api_key.clone(),
id_token: IdToken::new(response_payload.id_token),
expires_in: ExpiresIn::parse(response_payload.expires_in)?,
refresh_token: RefreshToken::new(response_payload.refresh_token),
})
}
pub async fn exchange_refresh_token(
&self,
refresh_token: RefreshToken,
) -> Result<Session> {
let request_payload = api::ExchangeRefreshTokenRequestBodyPayload::new(
refresh_token
.inner()
.to_string(),
);
let response_payload = api::exchange_refresh_token(
&self.client,
&self.api_key,
request_payload,
)
.await?;
Ok(Session {
client: self.client.clone(),
api_key: self.api_key.clone(),
id_token: IdToken::new(response_payload.id_token),
expires_in: ExpiresIn::parse(response_payload.expires_in)?,
refresh_token: RefreshToken::new(response_payload.refresh_token),
})
}
pub async fn fetch_providers_for_email(
&self,
email: Email,
continue_uri: OAuthContinueUri,
) -> Result<Option<Vec<ProviderId>>> {
let request_payload =
api::FetchProvidersForEmailRequestBodyPayload::new(
email.inner().to_string(),
continue_uri
.inner()
.to_string(),
);
let response_payload = api::fetch_providers_for_email(
&self.client,
&self.api_key,
request_payload,
)
.await?;
match response_payload.all_providers {
| None => Ok(None),
| Some(providers) => {
let provider_ids = providers
.iter()
.map(|provider_id| ProviderId::parse(provider_id.clone()))
.collect();
Ok(Some(provider_ids))
},
}
}
pub async fn send_reset_password_email(
&self,
email: Email,
locale: Option<LanguageCode>,
) -> Result<()> {
let request_payload =
api::SendPasswordResetEmailRequestBodyPayload::new(
email.inner().to_string(),
);
api::send_password_reset_email(
&self.client,
&self.api_key,
request_payload,
locale,
)
.await?;
Ok(())
}
}