Skip to content

Commit

Permalink
Show better error message on user signup validation error. (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzernik authored Aug 4, 2022
1 parent 2dc480f commit deff540
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::util;
use rocket::fairing::AdHoc;
use rocket::State;
use rocket::{form::*, get, post, response::Redirect, routes};
use rocket_auth::prelude::Error;
use rocket_auth::Error::FormValidationErrors;
use rocket_auth::Users;
use rocket_auth::{Auth, Login, Signup, User};
use rocket_db_pools::Connection;
Expand Down Expand Up @@ -70,7 +72,7 @@ async fn post_signup(

auth.signup(&form)
.await
.map_err(|_| "failed to signup user.")?;
.map_err(|e| format!("failed to signup user: {}", validation_error_message(&e)))?;
auth.login(&form.clone().into())
.await
.map_err(|_| "failed to signup user.")?;
Expand Down Expand Up @@ -149,6 +151,25 @@ async fn create_user_account(
Ok(())
}

fn validation_error_message(error: &Error) -> String {
match error {
FormValidationErrors(source) => {
source
.field_errors()
.into_iter()
.map(|(_, error)| error)
.map(IntoIterator::into_iter)
.map(|errs| {
errs //
.map(|err| &err.code)
.fold(String::new(), |a, b| a + b)
})
.fold(String::new(), |a, b| a + &b)
}
_ => "undefined".into(),
}
}

pub fn auth_stage() -> AdHoc {
AdHoc::on_ignite("Auth Stage", |rocket| async {
rocket.register("/", catchers![not_authorized]).mount(
Expand Down

0 comments on commit deff540

Please sign in to comment.