-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently only validates inputs and creates the user. Still needs authentication and anonymous user merging before being complete. To facilitate the implementation, multiple other crates were pulled in and features were implemented, notably: - Struct validation was implemented via Validator, and Axum's flexible API was used to generate responses. - Error responses in the format expected by the client (generic / specific errors) were implemented. - rs-snowflake was used to generate snowflake IDs. - chrono was used for dates and times. - bcrypt was used for password hashing.
- Loading branch information
Showing
11 changed files
with
438 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use sea_orm::DatabaseConnection; | ||
use snowflake::SnowflakeIdBucket; | ||
|
||
#[derive(Clone)] | ||
pub struct AppState { | ||
pub connection: DatabaseConnection, | ||
pub user_snowflake: SnowflakeIdBucket, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod errors; | ||
pub mod user; | ||
pub mod validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::models::user; | ||
use axum::http::StatusCode; | ||
use axum::response::{IntoResponse, Response}; | ||
use lazy_static::lazy_static; | ||
use sea_orm::DbErr; | ||
use serde::Serialize; | ||
use serde_json::json; | ||
use std::collections::HashMap; | ||
use thiserror::Error; | ||
|
||
lazy_static! { | ||
pub static ref FAILED_VALIDATION: Errors = | ||
Errors::generic("Some inputs failed validation.".to_string()); | ||
} | ||
|
||
#[derive(Serialize, Default, Clone)] | ||
pub struct Errors { | ||
generic: Vec<String>, | ||
specific: HashMap<String, Vec<String>>, | ||
} | ||
|
||
impl Errors { | ||
pub fn generic(value: String) -> Self { | ||
let mut errors = Self::default(); | ||
errors.add_generic(value); | ||
errors | ||
} | ||
|
||
pub fn add_generic(&mut self, value: String) { | ||
self.generic.push(value); | ||
} | ||
|
||
pub fn add_one_specific(&mut self, key: String, value: String) { | ||
self.add_specific(key, vec![value]); | ||
} | ||
|
||
pub fn add_specific(&mut self, key: String, value: Vec<String>) { | ||
self.specific.insert(key, value); | ||
} | ||
} | ||
|
||
impl IntoResponse for Errors { | ||
fn into_response(self) -> Response { | ||
json!({"errors": &self}).to_string().into_response() | ||
} | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum HandlerError { | ||
#[error("Username must not be taken.")] | ||
UsernameTaken, | ||
#[error("Email Address must not be taken.")] | ||
EmailTaken, | ||
|
||
#[error("Database transaction failed.")] | ||
Database(#[from] DbErr), | ||
#[error("Could not create user.")] | ||
CreateUser(#[from] user::CreateError), | ||
} | ||
|
||
impl HandlerError { | ||
fn into_generic(self, code: StatusCode) -> (StatusCode, Errors) { | ||
(code, Errors::generic(self.to_string())) | ||
} | ||
|
||
fn failed_validation(self, code: StatusCode, key: &str) -> (StatusCode, Errors) { | ||
let mut errors = FAILED_VALIDATION.clone(); | ||
errors.add_one_specific(key.to_string(), self.to_string()); | ||
(code, errors) | ||
} | ||
} | ||
|
||
impl IntoResponse for HandlerError { | ||
fn into_response(self) -> Response { | ||
match self { | ||
HandlerError::UsernameTaken => self.failed_validation(StatusCode::BAD_REQUEST, "username"), | ||
HandlerError::EmailTaken => self.failed_validation(StatusCode::BAD_REQUEST, "emailAddress"), | ||
HandlerError::Database(_) => self.into_generic(StatusCode::INTERNAL_SERVER_ERROR), | ||
HandlerError::CreateUser(_) => self.into_generic(StatusCode::INTERNAL_SERVER_ERROR), | ||
} | ||
.into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::app_state::AppState; | ||
use crate::controllers::errors::HandlerError; | ||
use crate::controllers::validate::ValidatedJson; | ||
use crate::models::user::{self, CreateUser}; | ||
use axum::extract::State; | ||
use axum::http::StatusCode; | ||
use serde::Deserialize; | ||
use validator::Validate; | ||
|
||
#[derive(Deserialize, Validate)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct RegisterUser { | ||
#[validate(length(min = 2, max = 32))] | ||
username: String, | ||
#[validate(email)] | ||
email_address: String, | ||
#[validate(length(min = 6, max = 72))] | ||
password: String, | ||
#[validate(must_match(other = "password", message = "Password"))] | ||
repeat_password: String, | ||
} | ||
|
||
pub async fn register( | ||
State(mut state): State<AppState>, | ||
ValidatedJson(details): ValidatedJson<RegisterUser>, | ||
) -> Result<StatusCode, HandlerError> { | ||
if user::name_exists(&state, &details.username).await? { | ||
return Err(HandlerError::UsernameTaken); | ||
} | ||
if user::email_exists(&state, &details.email_address).await? { | ||
return Err(HandlerError::EmailTaken); | ||
} | ||
|
||
user::create( | ||
&mut state, | ||
CreateUser { | ||
username: &details.username, | ||
email_address: &details.email_address, | ||
password: &details.password, | ||
}, | ||
) | ||
.await?; | ||
|
||
Ok(StatusCode::CREATED) | ||
} |
Oops, something went wrong.