From da5eefa73ab0ac552a8193543e433e8157d7881f Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 16 May 2023 11:55:32 -0700 Subject: [PATCH] add check for uniqueness violation in account creation --- src/users.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/users.rs b/src/users.rs index 96ab8bd..38cca2e 100644 --- a/src/users.rs +++ b/src/users.rs @@ -17,7 +17,7 @@ pub struct User { } #[derive(Debug, Clone, sqlx::FromRow, sqlx::Encode)] -struct DbUser { +pub(crate) struct DbUser { id: Uuid, username: String, displayname: Option, @@ -54,7 +54,7 @@ pub async fn create_user( .to_string(); let id = Uuid::new_v4(); - let id_bytes = sqlx::types::Uuid::from_u128(id.as_u128()); + let id_bytes = id.as_bytes().as_slice(); let res = sqlx::query(CREATE_QUERY) .bind(id_bytes) .bind(username) @@ -76,7 +76,9 @@ pub async fn create_user( } Err(sqlx::Error::Database(db)) => { if let Some(exit) = db.code() { - if exit.parse().unwrap_or(0) == 2067u32 { + let exit = exit.parse().unwrap_or(0u32); + // https://www.sqlite.org/rescode.html codes for unique constraint violations: + if exit == 2067u32 || exit == 1555 { Err(CreateUserErrorKind::AlreadyExists.into()) } else { Err(CreateUserErrorKind::Unknown.into()) @@ -85,7 +87,7 @@ pub async fn create_user( Err(CreateUserErrorKind::Unknown.into()) } } - _ => unreachable!(), + _ => Err(CreateUserErrorKind::Unknown.into()), } } @@ -97,9 +99,7 @@ pub struct CreateUserError(#[from] CreateUserErrorKind); #[non_exhaustive] pub enum CreateUserErrorKind { AlreadyExists, - #[error( - desc = "Usernames must be less than 20 characters and contain only ascii letters, numbers, and dashes." - )] + #[error(desc = "Usernames must be less than 20 characters long")] BadUsername, PasswordMismatch, MissingFields,