diff options
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/auth.rs | 11 | ||||
| -rw-r--r-- | src/models/user.rs | 30 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/models/auth.rs b/src/models/auth.rs index 8a70244..651bf26 100644 --- a/src/models/auth.rs +++ b/src/models/auth.rs @@ -32,13 +32,22 @@ pub struct AuthBody { token_type: String, } -/// Payload used for user creation +/// Payload used for login #[derive(Deserialize)] pub struct LoginCredentials { pub username: String, pub password: String, } +/// Paylod used for user creation +#[derive(Deserialize)] +pub struct SignUpForm { + pub email: String, + pub username: String, + pub password1: String, + pub password2: String, +} + static KEYS: Lazy<Keys> = Lazy::new(|| { let secret = &crate::config::CONFIG.jwt_secret; Keys::new(secret.as_bytes()) diff --git a/src/models/user.rs b/src/models/user.rs index ba2df8f..56ae307 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -131,4 +131,34 @@ impl User { Ok(row.count.unwrap()) } + + /// Prevent the "uniquess" Postgres fields check. Check if username has been taken + pub async fn username_has_taken(username: &String) -> Result<bool, AppError> { + let pool = unsafe { get_client() }; + let row = sqlx::query!( + r#" + SELECT COUNT(id) as count FROM users WHERE username = $1 + "#, + username, + ) + .fetch_one(pool) + .await?; + + Ok(row.count.unwrap() > 0) + } + + /// Prevent the "uniquess" Postgres fields check. Check if email has been taken + pub async fn email_has_taken(email: &String) -> Result<bool, AppError> { + let pool = unsafe { get_client() }; + let row = sqlx::query!( + r#" + SELECT COUNT(id) as count FROM users WHERE email = $1 + "#, + email, + ) + .fetch_one(pool) + .await?; + + Ok(row.count.unwrap() > 0) + } } |
