feat: convert empty emails to None in register form handler

SUMMARY
    Browsers send empty strings for empty fields in form submissions.
    Email is optional in registration process but empty field fails
    validation for email and results in 400 bad request. Converting
    empty strings to None fixes this.
master
Aravinth Manivannan 2022-02-23 08:08:02 +05:30
parent fdbd62a0a6
commit 78d24af48c
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 23 additions and 1 deletions

View File

@ -71,10 +71,15 @@ pub fn services(cfg: &mut web::ServiceConfig) {
#[my_codegen::post(path = "PAGES.auth.register")]
pub async fn register_submit(
payload: web::Form<RegisterPayload>,
mut payload: web::Form<RegisterPayload>,
data: AppData,
db: crate::DB,
) -> PageResult<impl Responder, Register> {
if let Some(email) = &payload.email {
if email.is_empty() {
payload.email = None;
}
}
data.register(&(**db), &payload)
.await
.map_err(|e| PageError::new(Register::new(&data.settings, Some(&payload)), e))?;

View File

@ -67,6 +67,23 @@ async fn auth_works(data: Arc<Data>, db: BoxDB) {
assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login);
let _ = data.delete_user(&db, NAME, PASSWORD).await;
// 1. Register with email == "" // Form request handler converts empty emails to None
let msg = Register {
username: NAME.into(),
password: PASSWORD.into(),
confirm_password: PASSWORD.into(),
email: Some("".into()),
};
let resp = test::call_service(
&app,
post_request!(&msg, PAGES.auth.register, FORM).to_request(),
)
.await;
assert_eq!(resp.status(), StatusCode::FOUND);
let headers = resp.headers();
assert_eq!(headers.get(header::LOCATION).unwrap(), PAGES.auth.login);
let _ = data.delete_user(&db, NAME, PASSWORD).await;
// 1. Register with email
let msg = Register {
username: NAME.into(),