From bafc93f6af29c7172a66899b77841f399980a45f Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 2 Mar 2024 16:28:03 -0800 Subject: [PATCH] gets a 422 back from forgejo from user creation request --- src/handlers/handlers.rs | 17 +++++++++++++---- src/main.rs | 2 +- src/user.rs | 6 +++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/handlers/handlers.rs b/src/handlers/handlers.rs index ed3548f..311fd64 100644 --- a/src/handlers/handlers.rs +++ b/src/handlers/handlers.rs @@ -36,6 +36,7 @@ pub async fn post_signup( Form(form): Form, ) -> Result { let user = validate_signup(&form).await?; + dbg!(&*SIGNUP_KEY, &user); session.insert(&SIGNUP_KEY, user).await.unwrap(); Ok(Redirect::to( @@ -46,7 +47,8 @@ pub async fn post_signup( /// Redirected from Stripe with the receipt of payment. pub async fn payment_success(session: Session, receipt: Option>) -> impl IntoResponse { let user: User = session.get(&SIGNUP_KEY).await.unwrap().unwrap_or_default(); - + // dbg!(&session); + dbg!(&*SIGNUP_KEY, &user); if receipt.is_none() { log::info!("Got {:?} from the session, but no receipt.", &user); return CreateUserError(CreateUserErrorKind::BadPayment).into_response(); @@ -58,11 +60,15 @@ pub async fn payment_success(session: Session, receipt: Option>) -> return CreateUserError(CreateUserErrorKind::NoFormFound).into_response(); } - if !confirm_payment(&receipt) { + if confirm_payment(&receipt) { + log::info!("Confirmed payment from {}", &receipt); + } else { return CreateUserError(CreateUserErrorKind::BadPayment).into_response(); } - if !create_user(&user) { + if create_user(&user) { + log::info!("Created user {user:?}"); + } else { return CreateUserError(CreateUserErrorKind::AlreadyExists).into_response(); } // TODO: store the receipt into a durable store to prevent re-use after creating @@ -85,8 +91,11 @@ fn create_user(user: &User) -> bool { .expect("Could not find $ADD_USER_ENDPOINT in environment"); let auth_header = format!("token {token}"); let user: ForgejoUser = user.into(); - let resp = ureq::post(&format!("http://{url}/api/v1/admin/users")) + dbg!(&user); + let resp = ureq::post(&format!("{url}/api/v1/admin/users")) .set("Authorization", &auth_header) + .set("Content-Type", "application/json") + .set("accept", "application/json") .send_json(user) .unwrap(); resp.status() == 201 diff --git a/src/main.rs b/src/main.rs index fb09c29..a73830c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ async fn main() { // just for signups let session_store = MemoryStore::default(); let session_layer = SessionManagerLayer::new(session_store) - .with_secure(true) + .with_secure(false) .with_expiry(Expiry::OnInactivity(time::Duration::hours(2))); // the core application, defining the routes and handlers diff --git a/src/user.rs b/src/user.rs index 0455ea2..919d5b6 100644 --- a/src/user.rs +++ b/src/user.rs @@ -44,10 +44,10 @@ impl Display for User { } } -#[derive(Clone, PartialEq, Eq, Deserialize, Serialize, Default)] +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] pub struct ForgejoUser<'u> { pub username: &'u str, - pub full_name: Option<&'u str>, + pub full_name: String, pub email: &'u str, pub password: &'u str, pub must_change_password: bool, @@ -57,7 +57,7 @@ impl<'u> From<&'u User> for ForgejoUser<'u> { fn from(user: &'u User) -> Self { Self { username: &user.username, - full_name: user.displayname.as_deref(), + full_name: user.displayname.to_owned().unwrap_or(user.username.clone()), email: &user.email, password: &user.password, must_change_password: false,