From a587459a1817c0fc57b46df3f9c69567e1e775b7 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 12 Dec 2023 18:20:56 +0000 Subject: refactor: separate model and controller --- src/users.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/users.rs') 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, @@ -11,3 +15,22 @@ pub struct User { pub privacy: Privacy, pub admin: bool, } + +impl User { + pub fn new(username: String, password: String) -> Result { + 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 { + Ok(verify(password, &self.password)?) + } +} -- cgit