Register user
This commit is contained in:
parent
c31586573b
commit
ca023645c4
@ -65,3 +65,8 @@ pub const ERROR_TOO_MANY_CREDENTIALS: (&str, &str) = (
|
||||
"ERROR.TOO_MANY_CREDENTIALS",
|
||||
"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.",
|
||||
);
|
||||
|
@ -1,19 +1,25 @@
|
||||
use std::error::Error;
|
||||
use chrono::Utc;
|
||||
use log::{error, log};
|
||||
use tokio::task::JoinError;
|
||||
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::token::Token;
|
||||
use crate::domain::user::User;
|
||||
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;
|
||||
|
||||
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();
|
||||
// Validate user
|
||||
validate_user_for_creation(&user, &mut error_resources);
|
||||
// Find if user exists
|
||||
if user.credentials.len() > 3 {
|
||||
error_resources.push(ERROR_TOO_MANY_CREDENTIALS);
|
||||
|
||||
}
|
||||
for credential_dto in user.credentials.iter() {
|
||||
match get_credential(
|
||||
@ -41,36 +47,73 @@ pub async fn register_user(db_conn: &sqlx::PgPool, user: UserRegisterPayload) ->
|
||||
if error_resources.len() > 0 {
|
||||
return Err(error_resources);
|
||||
}
|
||||
|
||||
/* TODO:
|
||||
// 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);
|
||||
user_to_insert.password = hash_result.hash;
|
||||
user_to_insert.salt = hash_result.salt;
|
||||
let hash_result = hash_password(&user.password);
|
||||
let now = Utc::now();
|
||||
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
|
||||
match insert_user(&db_conn, &user_to_insert).await{
|
||||
Ok(resultrs) => {
|
||||
user_to_insert.id = resultrs.last_insert_id() as u32;
|
||||
match insert_user(&db_conn, user_to_insert).await{
|
||||
Ok(user) => {
|
||||
persisted_user = user;
|
||||
},
|
||||
Err(error) => {
|
||||
println!("Error while inserting user in database from create_user method. Log: {}", error);
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
Err(e) => {
|
||||
error!("{}", e);
|
||||
error_resources.push(("ERROR.DATABASE_ERROR", ""));
|
||||
return Err(error_resources);
|
||||
}};
|
||||
|
||||
// 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 =
|
||||
Token::new(user_to_insert.id,
|
||||
tokens.get(0).expect("Error. Token doesn't exist in list.").to_string(),
|
||||
tokens.get(1).expect("Error. Token doesn't exist in list.").to_string()
|
||||
);
|
||||
Token {
|
||||
id: 0,
|
||||
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
|
||||
match insert_token(&db_conn, &token_to_insert).await{
|
||||
Ok(resultrs) => {token_to_insert.id = resultrs.last_insert_id() as u32},
|
||||
Err(_e) => {return HttpResponse::InternalServerError().finish()}
|
||||
match insert_token(&db_conn, token_to_insert).await {
|
||||
Ok(persisted_token) => {
|
||||
Ok(persisted_token)
|
||||
},
|
||||
Err(e) => {
|
||||
error!("{}", e);
|
||||
error_resources.push(("ERROR.DATABASE_ERROR", ""));
|
||||
Err(error_resources)
|
||||
}
|
||||
}
|
||||
*/
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
mod hasher;
|
||||
pub mod hasher;
|
||||
|
Loading…
Reference in New Issue
Block a user