use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::error::Result;
use crate::http::HttpClient;
use crate::models::LoginResult;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoginBody {
pub email: String,
pub password: String,
}
impl LoginBody {
pub fn new<E, P>(email: E, password: P) -> Self
where
E: Into<String>,
P: Into<String>,
{
Self {
email: email.into(),
password: password.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SocialLoginBody {
pub provider: String,
pub token: serde_json::Value,
pub has_accepted_terms: bool,
}
impl SocialLoginBody {
pub fn new<P>(provider: P, token: impl Into<serde_json::Value>, accepted_terms: bool) -> Self
where
P: Into<String>,
{
Self {
provider: provider.into(),
token: token.into(),
has_accepted_terms: accepted_terms,
}
}
pub fn google(token: impl Into<serde_json::Value>, accepted_terms: bool) -> Self {
Self::new("google", token, accepted_terms)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangePasswordBody {
pub email: String,
#[serde(rename = "password")]
pub current_password: String,
pub new_password: String,
}
impl ChangePasswordBody {
pub fn new<E, P, N>(email: E, current_password: P, new_password: N) -> Self
where
E: Into<String>,
P: Into<String>,
N: Into<String>,
{
Self {
email: email.into(),
current_password: current_password.into(),
new_password: new_password.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RequestPasswordResetBody {
pub email: String,
}
impl RequestPasswordResetBody {
pub fn new<E: Into<String>>(email: E) -> Self {
Self {
email: email.into(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResetPasswordBody {
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
pub new_password: String,
}
impl ResetPasswordBody {
pub fn new<E, N>(email: E, new_password: N) -> Self
where
E: Into<String>,
N: Into<String>,
{
Self {
email: email.into(),
token: None,
new_password: new_password.into(),
}
}
pub fn token<S: Into<String>>(mut self, token: S) -> Self {
self.token = Some(token.into());
self
}
}
#[derive(Debug)]
pub struct AuthApi<'a> {
http: &'a HttpClient,
}
impl<'a> AuthApi<'a> {
pub(crate) fn new(http: &'a HttpClient) -> Self {
Self { http }
}
pub async fn login(&self, body: &LoginBody) -> Result<LoginResult> {
let req = self.http.request(Method::POST, "login")?.json(body);
self.http.send_envelope(req).await
}
pub async fn social_login(&self, body: &SocialLoginBody) -> Result<LoginResult> {
let req = self
.http
.request(Method::POST, "authentication/social-login")?
.json(body);
self.http.send_envelope(req).await
}
pub async fn change_password(&self, body: &ChangePasswordBody) -> Result<()> {
let req = self
.http
.request(Method::PUT, "authentication/change-password")?
.json(body);
self.http.send_no_content(req).await
}
pub async fn request_password_reset(&self, body: &RequestPasswordResetBody) -> Result<()> {
let req = self
.http
.request(Method::PUT, "authentication/request-password-reset")?
.json(body);
self.http.send_no_content(req).await
}
pub async fn reset_password(&self, body: &ResetPasswordBody) -> Result<()> {
let req = self
.http
.request(Method::PUT, "authentication/reset-password")?
.json(body);
self.http.send_no_content(req).await
}
}