rave/src/user.rs

30 lines
749 B
Rust
Raw Normal View History

use color_eyre::Result;
2023-10-08 19:53:42 +00:00
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
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
}
}
pub async fn get_user(pool: &SqlitePool, name: &str) -> Result<Option<User>> {
Ok(
sqlx::query_as!(User, "SELECT * FROM users WHERE name = ?", name)
.fetch_optional(pool)
.await?,
)
}