server (/auth/register): add validator

This commit is contained in:
MedzikUser 2022-04-30 21:56:06 +02:00
parent 2c25f60b46
commit 860fd76969
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
3 changed files with 27 additions and 0 deletions

View File

@ -15,6 +15,21 @@ pub async fn handle(
) -> Result<Json<Response>, ServerError> {
let request = validate_json::<Request>(request)?;
// username must contain at least 4 characters
if request.username.len() < 4 {
return Err(ServerError::AuthError(AuthError::UsernameTooShort));
}
// username must be less than 20 characters
if request.username.len() < 20 {
return Err(ServerError::AuthError(AuthError::UsernameTooLong));
}
// password must contain at least 8 characters
if request.password.len() < 8 {
return Err(ServerError::AuthError(AuthError::PasswordTooShort));
}
let user = User::new(&request.username, &request.password);
let response = match db.create_user(&user).await {

View File

@ -8,6 +8,15 @@ pub enum Error {
#[error("user already exists")]
UserAlreadyExists,
#[error("username is too short")]
UsernameTooShort,
#[error("username is too long")]
UsernameTooLong,
#[error("password is too short")]
PasswordTooShort,
#[error("generate jwt token")]
TokenGenerate,

View File

@ -43,6 +43,9 @@ impl axum::response::IntoResponse for ServerError {
Self::AuthError(ref err) => match err {
AuthError::UserNotFound => StatusCode::BAD_REQUEST,
AuthError::UserAlreadyExists => StatusCode::NOT_ACCEPTABLE,
AuthError::UsernameTooShort => StatusCode::NOT_ACCEPTABLE,
AuthError::UsernameTooLong => StatusCode::NOT_ACCEPTABLE,
AuthError::PasswordTooShort => StatusCode::NOT_ACCEPTABLE,
AuthError::TokenGenerate => StatusCode::INTERNAL_SERVER_ERROR,
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
AuthError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,