Remove username, just use email address

This commit is contained in:
Z. Charles Dziura 2024-10-31 14:37:55 -04:00
parent 40ff448d59
commit db7264c3b9
6 changed files with 19 additions and 29 deletions

View file

@ -21,16 +21,14 @@ VALUES
CREATE TABLE IF NOT EXISTS
public.user (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(97) NOT NULL,
name VARCHAR(255) NOT NULL,
status_id INT NOT NULL REFERENCES status(id) DEFAULT 2,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS user_username_uniq_idx ON public.user(username);
CREATE UNIQUE INDEX IF NOT EXISTS user_email_uniq_idx ON public.user(email);
CREATE TABLE IF NOT EXISTS

View file

@ -9,18 +9,16 @@ use super::DbPool;
#[derive(Clone)]
pub struct NewUserEntity {
pub username: String,
pub password: String,
pub email: String,
pub password: String,
pub name: String,
}
impl Debug for NewUserEntity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NewUserEntity")
.field("username", &self.username)
.field("password", &"********")
.field("email", &self.email)
.field("password", &"********")
.field("name", &self.name)
.finish()
}
@ -40,14 +38,12 @@ pub async fn insert_new_user(
new_user: NewUserEntity,
) -> Result<UserEntity, AppError> {
let NewUserEntity {
username,
password,
email,
password,
name,
} = new_user.clone();
sqlx::query_as::<_, UserEntity>("INSERT INTO public.user (username, email, password, name) VALUES ($1, $2, $3, $4) RETURNING id, username, email, name, status_id;")
.bind(username)
sqlx::query_as::<_, UserEntity>("INSERT INTO public.user (email, password, name) VALUES ($1, $2, $3) RETURNING id, email, name, status_id;")
.bind(email)
.bind(password)
.bind(name)
@ -65,14 +61,14 @@ pub struct UserIdAndHashedPasswordEntity {
pub password: String,
}
pub async fn get_username_and_password_by_username(
pub async fn get_username_and_password_by_email(
pool: &DbPool,
username: String,
email: String,
) -> Result<UserIdAndHashedPasswordEntity, AppError> {
sqlx::query_as::<_, UserIdAndHashedPasswordEntity>(
"SELECT id, password FROM public.user WHERE username = $1;",
"SELECT id, password FROM public.user WHERE email = $1;",
)
.bind(username)
.bind(email)
.fetch_one(pool)
.await
.map_err(|err| {

View file

@ -11,7 +11,7 @@ use pasetors::{keys::SymmetricKey, version4::V4};
use tracing::debug;
use crate::{
db::{get_username_and_password_by_username, DbPool, UserIdAndHashedPasswordEntity},
db::{get_username_and_password_by_email, DbPool, UserIdAndHashedPasswordEntity},
models::{ApiResponse, AppError, Session},
requests::{
auth::login::models::{AuthLoginResponse, AuthLoginTokenData},
@ -44,11 +44,11 @@ async fn auth_login_request(
) -> Result<Response, AppError> {
debug!(?body);
let AuthLoginRequest { username, password } = body;
let AuthLoginRequest { email, password } = body;
let UserIdAndHashedPasswordEntity {
id: user_id,
password: hashed_password,
} = get_username_and_password_by_username(db_pool, username).await?;
} = get_username_and_password_by_email(db_pool, email).await?;
verify_password(password, hashed_password)?;

View file

@ -6,14 +6,14 @@ use serde_with::serde_as;
#[serde_as]
#[derive(Deserialize)]
pub struct AuthLoginRequest {
pub username: String,
pub email: String,
pub password: String,
}
impl Debug for AuthLoginRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AuthLoginRequest")
.field("username", &self.username)
.field("email", &self.email)
.field("password", &"********")
.finish()
}

View file

@ -50,18 +50,16 @@ async fn register_new_user_request(
debug!(?body, send_verification_email);
let UserRegistrationRequest {
username,
password,
email,
password,
name,
} = body;
let hashed_password = hash_password(password);
let new_user = NewUserEntity {
username: username.clone(),
password: hashed_password.to_string(),
email,
password: hashed_password.to_string(),
name,
};
@ -73,7 +71,7 @@ async fn register_new_user_request(
} = insert_new_user(db_pool, new_user).await.map_err(|err| {
if err.is_duplicate_record() {
AppError::duplicate_record(
"There is already an account associated with this username or email address.",
"There is already an account associated with this email address.",
)
} else {
err

View file

@ -5,18 +5,16 @@ use serde::Deserialize;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserRegistrationRequest {
pub username: String,
pub password: String,
pub email: String,
pub password: String,
pub name: String,
}
impl Debug for UserRegistrationRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UserRegistrationRequest")
.field("username", &self.username)
.field("password", &"********")
.field("email", &self.email)
.field("password", &"********")
.field("name", &self.name)
.finish()
}