add user structs

This commit is contained in:
Joe Ardent 2023-05-12 15:36:19 -07:00
parent 0223242473
commit d87ade7980
2 changed files with 33 additions and 3 deletions

View File

@ -6,11 +6,11 @@
-- users -- users
create table if not exists witches ( create table if not exists witches (
id blob not null primary key, id blob not null primary key,
last_seen int,
username text not null unique, username text not null unique,
displayname text, displayname text,
email text, email text,
secret blob not null, -- encrypted password? need to figure auth out last_seen int,
pwhash blob not null,
created_at int not null default (unixepoch()), created_at int not null default (unixepoch()),
last_updated int not null default (unixepoch()) last_updated int not null default (unixepoch())
); );

View File

@ -4,7 +4,37 @@ use argon2::{
}; };
use uuid::Uuid; use uuid::Uuid;
fn create_user(username: &str, password: &[u8]) -> Result<Uuid, CreateUserError> { const CREATE_QUERY: &str = "insert into witches (id, username, pwhash) values ($1, $2, $3)";
pub struct User {
id: Uuid,
username: String,
displayname: Option<String>,
email: Option<String>,
}
#[derive(Debug, Clone, sqlx::FromRow, sqlx::Encode)]
struct DbUser {
id: Uuid,
username: String,
displayname: Option<String>,
email: Option<String>,
last_seen: Option<u64>,
pwhash: String,
}
impl From<DbUser> for User {
fn from(dbu: DbUser) -> Self {
User {
id: dbu.id,
username: dbu.username,
displayname: dbu.displayname,
email: dbu.email,
}
}
}
async fn create_user(username: &User, password: &[u8]) -> Result<Uuid, CreateUserError> {
// Argon2 with default params (Argon2id v19) // Argon2 with default params (Argon2id v19)
let argon2 = Argon2::default(); let argon2 = Argon2::default();