From d87ade7980376e7c3b59835ea0117e12e3a678f5 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Fri, 12 May 2023 15:36:19 -0700 Subject: [PATCH] add user structs --- migrations/20230426221940_init.up.sql | 4 ++-- src/users.rs | 32 ++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/migrations/20230426221940_init.up.sql b/migrations/20230426221940_init.up.sql index e7615bd..111a881 100644 --- a/migrations/20230426221940_init.up.sql +++ b/migrations/20230426221940_init.up.sql @@ -6,11 +6,11 @@ -- users create table if not exists witches ( id blob not null primary key, - last_seen int, username text not null unique, displayname 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()), last_updated int not null default (unixepoch()) ); diff --git a/src/users.rs b/src/users.rs index 5dbd329..5d25df9 100644 --- a/src/users.rs +++ b/src/users.rs @@ -4,7 +4,37 @@ use argon2::{ }; use uuid::Uuid; -fn create_user(username: &str, password: &[u8]) -> Result { +const CREATE_QUERY: &str = "insert into witches (id, username, pwhash) values ($1, $2, $3)"; + +pub struct User { + id: Uuid, + username: String, + displayname: Option, + email: Option, +} + +#[derive(Debug, Clone, sqlx::FromRow, sqlx::Encode)] +struct DbUser { + id: Uuid, + username: String, + displayname: Option, + email: Option, + last_seen: Option, + pwhash: String, +} + +impl From 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 { // Argon2 with default params (Argon2id v19) let argon2 = Argon2::default();