Make /api/v1/accounts return validation error if user already exists

This commit is contained in:
silverpill 2022-02-11 20:44:23 +00:00
parent 0198a944ac
commit e48a8999f5
2 changed files with 7 additions and 2 deletions

View file

@ -61,7 +61,7 @@ async fn main() -> std::io::Result<()> {
Environment::Production => {
let mut cors_config = Cors::default();
for origin in config.http_cors_allowlist.iter() {
cors_config = cors_config.allowed_origin(&origin);
cors_config = cors_config.allowed_origin(origin);
};
cors_config
.allowed_origin(&config.instance_url())

View file

@ -92,7 +92,12 @@ pub async fn create_account(
let user_data = account_data.into_inner()
.into_user_data(password_hash, private_key_pem);
let user = create_user(db_client, user_data).await?;
let user = match create_user(db_client, user_data).await {
Ok(user) => user,
Err(DatabaseError::AlreadyExists(_)) =>
return Err(ValidationError("user already exists").into()),
Err(other_error) => return Err(other_error.into()),
};
log::warn!("created user {}", user.id);
let account = Account::from_user(user, &config.instance_url());
Ok(HttpResponse::Created().json(account))