summaryrefslogtreecommitdiffstats
path: root/src/users.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/users.rs')
-rw-r--r--src/users.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/users.rs b/src/users.rs
index bf41fd5..4cf9310 100644
--- a/src/users.rs
+++ b/src/users.rs
@@ -1,8 +1,12 @@
+use bcrypt::hash;
+use bcrypt::verify;
+use bcrypt::DEFAULT_COST;
+
use crate::Privacy;
+use crate::Result;
#[derive(sqlx::FromRow)]
pub struct User {
- pub id: i32,
pub username: String,
pub password: String,
pub email: Option<String>,
@@ -11,3 +15,22 @@ pub struct User {
pub privacy: Privacy,
pub admin: bool,
}
+
+impl User {
+ pub fn new(username: String, password: String) -> Result<Self> {
+ let password_hash = hash(password, DEFAULT_COST)?;
+ Ok(Self {
+ username,
+ password: password_hash,
+ email: None,
+ bio: None,
+ site: None,
+ privacy: Privacy::Public,
+ admin: true,
+ })
+ }
+
+ pub fn verify_password(&self, password: &str) -> Result<bool> {
+ Ok(verify(password, &self.password)?)
+ }
+}