Made AuthEmail, Password and Username reusable

This commit is contained in:
bitfl0wer 2023-04-23 11:58:45 +02:00
parent 2e52fb71d4
commit 1e00296dc9
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
3 changed files with 88 additions and 21 deletions

View File

@ -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) {}
}
}

View File

@ -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<Token, InstanceServerError> {
@ -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(&reg).await.err().unwrap()
test_instance.register_account(&reg).await.err().unwrap()
);
}
@ -120,7 +120,7 @@ mod test {
None,
)
.unwrap();
let token = test_instance.register(&reg).await.unwrap().token;
let token = test_instance.register_account(&reg).await.unwrap().token;
println!("{}", token);
}
}

View File

@ -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<AuthEmail, FieldFormatError>`].
## 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<AuthEmail, FieldFormatError> {
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<AuthUsername, FieldFormatError> {
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<AuthPassword, FieldFormatError> {
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<String>,
username: AuthUsername,
password: Option<AuthPassword>,
consent: bool,
email: Option<String>,
email: Option<AuthEmail>,
fingerprint: Option<String>,
invite: Option<String>,
date_of_birth: Option<String>,
@ -45,28 +102,31 @@ pub mod schemas {
captcha_key: Option<String>,
promotional_email_opt_in: Option<bool>,
) -> Result<RegisterSchema, FieldFormatError> {
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,