auth-login / src /models.rs
amy-invitation
main
b7fdfb4
Raw History Blame Contribute Delete
1.02 kB
use argon2::{
Argon2, PasswordHasher,
password_hash::{SaltString, rand_core::OsRng},
};
use serde::Deserialize;
use sqlx::prelude::FromRow;
#[derive(Deserialize)]
pub struct UserRequest {
username: String,
password: String,
}
#[derive(FromRow, Deserialize)]
pub struct UserSql {
id: i32,
username: String,
password: String,
}
trait Checked {
fn username(&self) -> &String;
fn password(&self) -> &String;
fn is_null(&self) -> bool {
self.username().trim() == "" || self.password().trim() == ""
}
fn hash_password(&self) -> String {
let salt = SaltString::generate(&mut OsRng);
let argon2 = Argon2::default();
let hashed_password = argon2
.hash_password(self.password().as_bytes(), &salt)
.unwrap()
.to_string();
hashed_password
}
}
impl Checked for UserSql {
fn username(&self) -> &String {
&self.username
}
fn password(&self) -> &String {
&self.password
}
}