Made AuthEmail, Password and Username reusable
This commit is contained in:
parent
2e52fb71d4
commit
1e00296dc9
|
@ -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) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
pub mod register {
|
pub mod register {
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde_json::{from_str, json, Value};
|
use serde_json::json;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
api::{
|
api::{
|
||||||
|
@ -19,7 +19,7 @@ pub mod register {
|
||||||
# Errors
|
# Errors
|
||||||
* [`InstanceServerError`] - If the server does not respond.
|
* [`InstanceServerError`] - If the server does not respond.
|
||||||
*/
|
*/
|
||||||
pub async fn register(
|
pub async fn register_account(
|
||||||
&mut self,
|
&mut self,
|
||||||
register_schema: &RegisterSchema,
|
register_schema: &RegisterSchema,
|
||||||
) -> Result<Token, InstanceServerError> {
|
) -> Result<Token, InstanceServerError> {
|
||||||
|
@ -92,7 +92,7 @@ mod test {
|
||||||
error_type: "date_of_birth".to_string(),
|
error_type: "date_of_birth".to_string(),
|
||||||
error: "This field is required (BASE_TYPE_REQUIRED)".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,
|
None,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let token = test_instance.register(®).await.unwrap().token;
|
let token = test_instance.register_account(®).await.unwrap().token;
|
||||||
println!("{}", token);
|
println!("{}", token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,63 @@ pub mod schemas {
|
||||||
|
|
||||||
use crate::errors::FieldFormatError;
|
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)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
#[serde(rename_all = "snake_case")]
|
#[serde(rename_all = "snake_case")]
|
||||||
pub struct RegisterSchema {
|
pub struct RegisterSchema {
|
||||||
|
@ -34,10 +91,10 @@ pub mod schemas {
|
||||||
These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/)
|
These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/)
|
||||||
*/
|
*/
|
||||||
pub fn new(
|
pub fn new(
|
||||||
username: String,
|
username: AuthUsername,
|
||||||
password: Option<String>,
|
password: Option<AuthPassword>,
|
||||||
consent: bool,
|
consent: bool,
|
||||||
email: Option<String>,
|
email: Option<AuthEmail>,
|
||||||
fingerprint: Option<String>,
|
fingerprint: Option<String>,
|
||||||
invite: Option<String>,
|
invite: Option<String>,
|
||||||
date_of_birth: Option<String>,
|
date_of_birth: Option<String>,
|
||||||
|
@ -45,28 +102,31 @@ pub mod schemas {
|
||||||
captcha_key: Option<String>,
|
captcha_key: Option<String>,
|
||||||
promotional_email_opt_in: Option<bool>,
|
promotional_email_opt_in: Option<bool>,
|
||||||
) -> Result<RegisterSchema, FieldFormatError> {
|
) -> Result<RegisterSchema, FieldFormatError> {
|
||||||
if username.len() < 2 || username.len() > 32 {
|
let username = username.username;
|
||||||
return Err(FieldFormatError::UsernameError);
|
|
||||||
|
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)
|
let has_password;
|
||||||
{
|
if password.is_some() {
|
||||||
return Err(FieldFormatError::PasswordError);
|
has_password = Some(password.unwrap().password);
|
||||||
|
} else {
|
||||||
|
has_password = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !consent {
|
if !consent {
|
||||||
return Err(FieldFormatError::ConsentError);
|
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 {
|
return Ok(RegisterSchema {
|
||||||
username,
|
username,
|
||||||
password,
|
password: has_password,
|
||||||
consent,
|
consent,
|
||||||
email,
|
email: email_addr,
|
||||||
fingerprint,
|
fingerprint,
|
||||||
invite,
|
invite,
|
||||||
date_of_birth,
|
date_of_birth,
|
||||||
|
|
Loading…
Reference in New Issue