summaryrefslogtreecommitdiffstats
path: root/src/models
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-13 12:51:32 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-13 12:51:32 +0000
commit64d5f37d9e28363f27b49b9a5b421a19792d0165 (patch)
tree210cbea0a4b17adee8c8916c3089a18fb9c77fbc /src/models
parentce62e3bac6a8acea555731d6222028c511dd9019 (diff)
Signup route
Diffstat (limited to 'src/models')
-rw-r--r--src/models/auth.rs11
-rw-r--r--src/models/user.rs30
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)
+ }
}