server (/auth/register): add validator
This commit is contained in:
parent
2c25f60b46
commit
860fd76969
|
@ -15,6 +15,21 @@ pub async fn handle(
|
||||||
) -> Result<Json<Response>, ServerError> {
|
) -> Result<Json<Response>, ServerError> {
|
||||||
let request = validate_json::<Request>(request)?;
|
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 user = User::new(&request.username, &request.password);
|
||||||
|
|
||||||
let response = match db.create_user(&user).await {
|
let response = match db.create_user(&user).await {
|
||||||
|
|
|
@ -8,6 +8,15 @@ pub enum Error {
|
||||||
#[error("user already exists")]
|
#[error("user already exists")]
|
||||||
UserAlreadyExists,
|
UserAlreadyExists,
|
||||||
|
|
||||||
|
#[error("username is too short")]
|
||||||
|
UsernameTooShort,
|
||||||
|
|
||||||
|
#[error("username is too long")]
|
||||||
|
UsernameTooLong,
|
||||||
|
|
||||||
|
#[error("password is too short")]
|
||||||
|
PasswordTooShort,
|
||||||
|
|
||||||
#[error("generate jwt token")]
|
#[error("generate jwt token")]
|
||||||
TokenGenerate,
|
TokenGenerate,
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,9 @@ impl axum::response::IntoResponse for ServerError {
|
||||||
Self::AuthError(ref err) => match err {
|
Self::AuthError(ref err) => match err {
|
||||||
AuthError::UserNotFound => StatusCode::BAD_REQUEST,
|
AuthError::UserNotFound => StatusCode::BAD_REQUEST,
|
||||||
AuthError::UserAlreadyExists => StatusCode::NOT_ACCEPTABLE,
|
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::TokenGenerate => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
|
AuthError::InvalidToken => StatusCode::BAD_REQUEST,
|
||||||
AuthError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
AuthError::UnknowError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
|
Loading…
Reference in New Issue