diff --git a/src/main.rs b/src/main.rs index 0fd52c9..2338bde 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,33 +53,42 @@ async fn createuser( mut db: Connection, info: Json, cookies: &CookieJar<'_>, -) -> status::Custom<&'static str> { +) -> status::Custom { let token = cookies.get_private("token"); match token.is_some() { - true => { - status::Custom(Status::Forbidden, - "You're already logged in. Log out before trying to create a new account.") - } + true => status::Custom( + Status::Forbidden, + "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 { - 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 { Ok(_) => match User::get_by_username(&mut db, &info.username).await { Some(user) => match user.set_new_token(&mut db).await { Ok(t) => { cookies.add_private(("token", t)); - "Your account has been created and you've been automatically logged in." - .to_string() + status::Custom(Status::Ok, "Your account has been created and you've been automatically logged in.".to_string()) } Err(why) => { eprintln!("{why:?}"); - "Couldn't log you in. Your account has been created, though." - .to_string() + status::Custom( + Status::Created, + "Couldn't log you in. Your account has been created, though." + .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) => { - format!("Couldn't create user: {}", why) + format!("Couldn't create user: {}", why); + status::Custom(Status::InternalServerError, format!("{why}")) } }, },