This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Alexandria/src/entities/user.rs

35 lines
994 B
Rust

use argon2::{Argon2, PasswordHash, PasswordVerifier};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "User")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub id: u32,
#[sea_orm(unique)]
pub username: String,
pub hashed_password: String,
pub current_bal_id: Option<u32>,
}
impl Model {
pub fn verify_password(&self, password: String) -> bool {
let parsed_hash = PasswordHash::new(&self.hashed_password).unwrap();
Argon2::default().verify_password(password.as_bytes(), &parsed_hash).is_ok()
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::owner::Entity")]
Owner,
}
impl Related<super::owner::Entity> for Entity {
fn to() -> RelationDef {
Relation::Owner.def()
}
}
impl ActiveModelBehavior for ActiveModel {}