update POST /createuser error messages

This commit is contained in:
SadlyNotSappho 2024-03-19 13:13:58 -07:00
parent 57d558e254
commit 6e69be35b8
1 changed files with 21 additions and 12 deletions

View File

@ -53,33 +53,42 @@ async fn createuser(
mut db: Connection<Db>, mut db: Connection<Db>,
info: Json<LoginInfo>, info: Json<LoginInfo>,
cookies: &CookieJar<'_>, cookies: &CookieJar<'_>,
) -> status::Custom<&'static str> { ) -> status::Custom<String> {
let token = cookies.get_private("token"); let token = cookies.get_private("token");
match token.is_some() { match token.is_some() {
true => { true => status::Custom(
status::Custom(Status::Forbidden, Status::Forbidden,
"You're already logged in. Log out before trying to create a new account.") "You're already logged in. Log out before trying to create a new account.".to_string(),
} ),
false => match User::get_by_username(&mut db, &info.username).await { false => match User::get_by_username(&mut db, &info.username).await {
Some(_) => "Username already taken. Please try again.".to_string(), Some(_) => status::Custom(
Status::Forbidden,
"Username already taken. Please try again.".to_string(),
),
None => match User::create(&mut db, &info.username, &info.password).await { None => match User::create(&mut db, &info.username, &info.password).await {
Ok(_) => match User::get_by_username(&mut db, &info.username).await { Ok(_) => match User::get_by_username(&mut db, &info.username).await {
Some(user) => match user.set_new_token(&mut db).await { Some(user) => match user.set_new_token(&mut db).await {
Ok(t) => { Ok(t) => {
cookies.add_private(("token", t)); cookies.add_private(("token", t));
"Your account has been created and you've been automatically logged in." status::Custom(Status::Ok, "Your account has been created and you've been automatically logged in.".to_string())
.to_string()
} }
Err(why) => { Err(why) => {
eprintln!("{why:?}"); eprintln!("{why:?}");
status::Custom(
Status::Created,
"Couldn't log you in. Your account has been created, though." "Couldn't log you in. Your account has been created, though."
.to_string() .to_string(),
)
} }
}, },
None => "Something went really wrong. I don't know what.".to_string(), None => status::Custom(
Status::InternalServerError,
"Something went really wrong. I don't know what.".to_string(),
),
}, },
Err(why) => { Err(why) => {
format!("Couldn't create user: {}", why) format!("Couldn't create user: {}", why);
status::Custom(Status::InternalServerError, format!("{why}"))
} }
}, },
}, },