rave/src/user.rs

31 lines
763 B
Rust
Raw Normal View History

use color_eyre::Result;
2023-10-08 19:53:42 +00:00
use serde::{Deserialize, Serialize};
2023-10-09 13:38:30 +00:00
use crate::utils::middleware::DbConn;
2023-10-08 19:53:42 +00:00
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct User {
pub id: i64,
2023-10-08 19:53:42 +00:00
pub name: String,
/// I hate this. It's stored in plaintext. Why?
password: String,
2023-10-08 19:53:42 +00:00
pub is_admin: bool,
}
impl User {
pub fn verify(&self, hashed_password: &str, salt: &str) -> bool {
let ours = md5::compute(format!("{}{}", self.password, salt));
let ours = format!("{ours:x}");
ours == hashed_password
}
}
2023-10-09 13:38:30 +00:00
pub async fn get_user(conn: DbConn, name: &str) -> Result<Option<User>> {
let user = sqlx::query_as!(User, "SELECT * FROM users WHERE name = $1", name)
.fetch_optional(&*conn)
.await?;
Ok(user)
}