go back to uuids for users

This commit is contained in:
Joe Ardent 2023-05-28 12:58:26 -07:00
parent b1fd57c486
commit 41a7abbe13
3 changed files with 9 additions and 24 deletions

View File

@ -5,7 +5,7 @@
-- users -- users
create table if not exists witches ( create table if not exists witches (
id int not null primary key, id blob not null primary key,
username text not null unique, username text not null unique,
displayname text, displayname text,
email text, email text,

View File

@ -6,13 +6,3 @@ pub mod generic_handlers;
pub mod session_store; pub mod session_store;
pub(crate) mod templates; pub(crate) mod templates;
pub mod users; pub mod users;
pub trait ToBlob {
fn blob(&self) -> &[u8];
}
impl ToBlob for uuid::Uuid {
fn blob(&self) -> &[u8] {
self.as_bytes().as_slice()
}
}

View File

@ -11,15 +11,11 @@ use axum::{
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore}; use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore};
use rand_core::CryptoRngCore;
use sqlx::{query_as, SqlitePool}; use sqlx::{query_as, SqlitePool};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::templates::{CreateUser, LoginGet};
templates::{CreateUser, LoginGet},
ToBlob,
};
const CREATE_QUERY: &str = const CREATE_QUERY: &str =
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)"; "insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
@ -28,7 +24,7 @@ const ID_QUERY: &str = "select * from witches where id = $1";
#[derive(Debug, Default, Clone, PartialEq, Eq, sqlx::FromRow)] #[derive(Debug, Default, Clone, PartialEq, Eq, sqlx::FromRow)]
pub struct User { pub struct User {
pub id: i64, pub id: Uuid,
pub username: String, pub username: String,
pub displayname: Option<String>, pub displayname: Option<String>,
pub email: Option<String>, pub email: Option<String>,
@ -49,10 +45,10 @@ impl Display for User {
} }
} }
pub type AuthContext = axum_login::extractors::AuthContext<i64, User, SqliteStore<User>>; pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
impl AuthUser<i64> for User { impl AuthUser<Uuid> for User {
fn get_id(&self) -> i64 { fn get_id(&self) -> Uuid {
self.id self.id
} }
@ -170,7 +166,7 @@ pub async fn post_create_user(
let user = create_user(username, displayname, email, password, &pool).await?; let user = create_user(username, displayname, email, password, &pool).await?;
tracing::debug!("created {user:?}"); tracing::debug!("created {user:?}");
let id = user.id; let id = user.id.as_simple().to_string();
let location = format!("/signup_success/{id}"); let location = format!("/signup_success/{id}");
let resp = axum::response::Redirect::temporary(&location).into_response(); let resp = axum::response::Redirect::temporary(&location).into_response();
@ -187,7 +183,7 @@ pub async fn handle_signup_success(
let id = id.trim(); let id = id.trim();
let id = Uuid::try_parse(id).unwrap_or_default(); let id = Uuid::try_parse(id).unwrap_or_default();
query_as(ID_QUERY) query_as(ID_QUERY)
.bind(id.blob()) .bind(id)
.fetch_one(&pool) .fetch_one(&pool)
.await .await
.unwrap_or_default() .unwrap_or_default()
@ -266,8 +262,7 @@ async fn create_user(
.unwrap() // safe to unwrap, we know the salt is valid .unwrap() // safe to unwrap, we know the salt is valid
.to_string(); .to_string();
let mut rng = OsRng; let id = Uuid::new_v4();
let id: i64 = rng.as_rngcore().next_u64() as i64;
let res = sqlx::query(CREATE_QUERY) let res = sqlx::query(CREATE_QUERY)
.bind(id) .bind(id)
.bind(username) .bind(username)