Register user

This commit is contained in:
Franklin 2023-09-21 09:23:43 -04:00
parent c31586573b
commit ca023645c4
3 changed files with 73 additions and 25 deletions

View File

@ -65,3 +65,8 @@ pub const ERROR_TOO_MANY_CREDENTIALS: (&str, &str) = (
"ERROR.TOO_MANY_CREDENTIALS", "ERROR.TOO_MANY_CREDENTIALS",
"Only up to 3 credentials are allowed. One of each type.", "Only up to 3 credentials are allowed. One of each type.",
); );
pub const ERROR_TOKEN_NOT_CREATED: (&str, &str) = (
"ERROR.TOKEN_NOT_CREATED",
"Token futures were joined but not created correctly.",
);

View File

@ -1,19 +1,25 @@
use std::error::Error; use std::error::Error;
use chrono::Utc;
use log::{error, log}; use log::{error, log};
use tokio::task::JoinError;
use crate::dao::credential::get_credential; use crate::dao::credential::get_credential;
use crate::dao::token::insert_token;
use crate::dao::user::insert_user;
use crate::domain::credential::Credential; use crate::domain::credential::Credential;
use crate::domain::token::Token;
use crate::domain::user::User;
use crate::dto::users::UserRegisterPayload; use crate::dto::users::UserRegisterPayload;
use crate::resources::error_messages::{ERROR_TOO_MANY_CREDENTIALS, ERROR_USER_ALREADY_EXISTS, ErrorResource}; use crate::resources::error_messages::{ERROR_TOKEN_NOT_CREATED, ERROR_TOO_MANY_CREDENTIALS, ERROR_USER_ALREADY_EXISTS, ErrorResource};
use crate::utils::hasher::{generate_multiple_random_token_with_rng, hash_password};
use crate::validation::user_validator::validate_user_for_creation; use crate::validation::user_validator::validate_user_for_creation;
pub async fn register_user(db_conn: &sqlx::PgPool, user: UserRegisterPayload) -> Result<(), Vec<ErrorResource>> { pub async fn register_user(db_conn: &sqlx::PgPool, user: UserRegisterPayload) -> Result<Token, Vec<ErrorResource>> {
let mut error_resources: Vec<ErrorResource> = Vec::new(); let mut error_resources: Vec<ErrorResource> = Vec::new();
// Validate user // Validate user
validate_user_for_creation(&user, &mut error_resources); validate_user_for_creation(&user, &mut error_resources);
// Find if user exists // Find if user exists
if user.credentials.len() > 3 { if user.credentials.len() > 3 {
error_resources.push(ERROR_TOO_MANY_CREDENTIALS); error_resources.push(ERROR_TOO_MANY_CREDENTIALS);
} }
for credential_dto in user.credentials.iter() { for credential_dto in user.credentials.iter() {
match get_credential( match get_credential(
@ -41,36 +47,73 @@ pub async fn register_user(db_conn: &sqlx::PgPool, user: UserRegisterPayload) ->
if error_resources.len() > 0 { if error_resources.len() > 0 {
return Err(error_resources); return Err(error_resources);
} }
/* TODO:
// Get salt and hashed password from hashing function then give the results to the user // Get salt and hashed password from hashing function then give the results to the user
let hash_result = hasher::hash_password(&user_to_insert.password); let hash_result = hash_password(&user.password);
user_to_insert.password = hash_result.hash; let now = Utc::now();
user_to_insert.salt = hash_result.salt; let mut user_to_insert = User {
id: 0,
name: user.name,
password: hash_result.hash,
salt: hash_result.salt,
time_created: now,
last_updated: now,
};
let persisted_user;
// Insert user in DB // Insert user in DB
match insert_user(&db_conn, &user_to_insert).await{ match insert_user(&db_conn, user_to_insert).await{
Ok(resultrs) => { Ok(user) => {
user_to_insert.id = resultrs.last_insert_id() as u32; persisted_user = user;
}, },
Err(error) => { Err(e) => {
println!("Error while inserting user in database from create_user method. Log: {}", error); error!("{}", e);
return HttpResponse::InternalServerError().finish(); error_resources.push(("ERROR.DATABASE_ERROR", ""));
return Err(error_resources);
}}; }};
// Create token and send it back. // Create token and send it back.
let tokens: Vec<String> = hasher::generate_multiple_random_token_with_rng(2).await.expect("Error creating multiple random tokens."); let mut tokens: Vec<String> = match generate_multiple_random_token_with_rng(2).await {
Ok(tokens) => tokens,
Err(e) => {
error!("{}", e);
error_resources.push(("ERROR.JOIN_ERROR", ""));
return Err(error_resources);
}
};
let mut token_to_insert = let mut token_to_insert =
Token::new(user_to_insert.id, Token {
tokens.get(0).expect("Error. Token doesn't exist in list.").to_string(), id: 0,
tokens.get(1).expect("Error. Token doesn't exist in list.").to_string() user_id: persisted_user.id,
); auth_token: match tokens.get(0) {
None => {
error!("Tokens were not created.", );
error_resources.push(ERROR_TOKEN_NOT_CREATED);
return Err(error_resources);
}
Some(token) => token.clone()
},
refresh_token: match tokens.get(1) {
None => {
error!("Tokens were not created.", );
error_resources.push(ERROR_TOKEN_NOT_CREATED);
return Err(error_resources);
}
Some(token) => token.clone()
},
time_created: now,
last_updated: now,
};
// Insert token in DB // Insert token in DB
match insert_token(&db_conn, &token_to_insert).await{ match insert_token(&db_conn, token_to_insert).await {
Ok(resultrs) => {token_to_insert.id = resultrs.last_insert_id() as u32}, Ok(persisted_token) => {
Err(_e) => {return HttpResponse::InternalServerError().finish()} Ok(persisted_token)
},
Err(e) => {
error!("{}", e);
error_resources.push(("ERROR.DATABASE_ERROR", ""));
Err(error_resources)
}
} }
*/
Ok(())
} }

View File

@ -1 +1 @@
mod hasher; pub mod hasher;