summaryrefslogtreecommitdiffstats
path: root/src/users.rs
blob: 4cf93102b5586b91509d8f08aad3571aee1da94f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use bcrypt::hash;
use bcrypt::verify;
use bcrypt::DEFAULT_COST;

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

#[derive(sqlx::FromRow)]
pub struct User {
    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)?;
        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)?)
    }
}