diff options
author | cel 🌸 <cel@blos.sm> | 2023-12-12 18:20:56 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-12-12 18:20:56 +0000 |
commit | a587459a1817c0fc57b46df3f9c69567e1e775b7 (patch) | |
tree | ab99c6aaa7c2b3245c83db6332e4ca54006831b3 /src/users.rs | |
parent | 370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0 (diff) | |
download | pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.gz pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.bz2 pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.zip |
refactor: separate model and controller
Diffstat (limited to '')
-rw-r--r-- | src/users.rs | 25 |
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)?) + } +} |