diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index ee8788e..a61ed8b 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -1 +1,8 @@ -pub mod login {} +pub mod login { + use crate::api::schemas::schemas::LoginSchema; + use crate::instance::Instance; + + impl Instance { + pub async fn login_account(&mut self, login_schema: &LoginSchema) {} + } +} diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 191480f..de78b88 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -1,6 +1,6 @@ pub mod register { use reqwest::Client; - use serde_json::{from_str, json, Value}; + use serde_json::json; use crate::{ api::{ @@ -19,7 +19,7 @@ pub mod register { # Errors * [`InstanceServerError`] - If the server does not respond. */ - pub async fn register( + pub async fn register_account( &mut self, register_schema: &RegisterSchema, ) -> Result { @@ -92,7 +92,7 @@ mod test { error_type: "date_of_birth".to_string(), error: "This field is required (BASE_TYPE_REQUIRED)".to_string() }, - test_instance.register(®).await.err().unwrap() + test_instance.register_account(®).await.err().unwrap() ); } @@ -120,7 +120,7 @@ mod test { None, ) .unwrap(); - let token = test_instance.register(®).await.unwrap().token; + let token = test_instance.register_account(®).await.unwrap().token; println!("{}", token); } } diff --git a/src/api/schemas.rs b/src/api/schemas.rs index e0a770d..1fdc9ab 100644 --- a/src/api/schemas.rs +++ b/src/api/schemas.rs @@ -5,6 +5,63 @@ pub mod schemas { use crate::errors::FieldFormatError; + /** + A struct that represents a well-formed email address. + */ + #[derive(Clone)] + pub struct AuthEmail { + pub email: String, + } + + impl AuthEmail { + /** + Returns a new [`Result`]. + ## Arguments + The email address you want to validate. + ## Errors + You will receive a [`FieldFormatError`], if: + - The email address is not in a valid format. + + */ + pub fn new(email: String) -> Result { + let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); + if !regex.is_match(email.clone().as_str()) { + return Err(FieldFormatError::EmailError); + } + return Ok(AuthEmail { email }); + } + } + + #[derive(Clone)] + pub struct AuthUsername { + pub username: String, + } + + impl AuthUsername { + pub fn new(username: String) -> Result { + if username.len() < 2 || username.len() > 32 { + return Err(FieldFormatError::UsernameError); + } else { + return Ok(AuthUsername { username }); + } + } + } + + #[derive(Clone)] + pub struct AuthPassword { + pub password: String, + } + + impl AuthPassword { + pub fn new(password: String) -> Result { + if password.len() < 1 || password.len() > 72 { + return Err(FieldFormatError::PasswordError); + } else { + return Ok(AuthPassword { password }); + } + } + } + #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] #[serde(rename_all = "snake_case")] pub struct RegisterSchema { @@ -34,10 +91,10 @@ pub mod schemas { These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/) */ pub fn new( - username: String, - password: Option, + username: AuthUsername, + password: Option, consent: bool, - email: Option, + email: Option, fingerprint: Option, invite: Option, date_of_birth: Option, @@ -45,28 +102,31 @@ pub mod schemas { captcha_key: Option, promotional_email_opt_in: Option, ) -> Result { - if username.len() < 2 || username.len() > 32 { - return Err(FieldFormatError::UsernameError); + let username = username.username; + + let email_addr; + if email.is_some() { + email_addr = Some(email.unwrap().email); + } else { + email_addr = None; } - if password.is_some() - && (password.as_ref().unwrap().len() < 1 || password.as_ref().unwrap().len() > 72) - { - return Err(FieldFormatError::PasswordError); + + let has_password; + if password.is_some() { + has_password = Some(password.unwrap().password); + } else { + has_password = None; } + if !consent { return Err(FieldFormatError::ConsentError); } - let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap(); - if email.clone().is_some() && !regex.is_match(email.clone().unwrap().as_str()) { - return Err(FieldFormatError::EmailError); - } - return Ok(RegisterSchema { username, - password, + password: has_password, consent, - email, + email: email_addr, fingerprint, invite, date_of_birth,