summaryrefslogblamecommitdiffstats
path: root/src/users.rs
blob: e8b0b674b1cb35c65a360c3232aff5b0ea40255a (plain) (tree)
1
2
3
4
5
6
7
8
9
10


                         
               
 
                   
                  


                        
                 







                              



                                                                    
                                
                 
               













                                                                   
use bcrypt::hash;
use bcrypt::verify;
use bcrypt::DEFAULT_COST;
use uuid::Uuid;

use crate::Privacy;
use crate::Result;

#[derive(sqlx::FromRow)]
pub struct User {
    pub id: Uuid,
    pub username: String,
    pub password: String,
    pub email: Option<String>,
    pub bio: Option<String>,
    pub site: Option<String>,
    pub privacy: Privacy,
    pub admin: bool,
}

impl User {
    pub fn new(username: String, password: String) -> Result<Self> {
        let password_hash = hash(password, DEFAULT_COST)?;
        let id = Uuid::new_v4();
        Ok(Self {
            id,
            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)?)
    }
}