How to redirect after successful form submission with axum?

Viewed 181

I found axum::response::Redirect to redirect a user to a different page, but the example there only shows get methods. I want to know how I can do that with post. The code I'm thinking is like the following:

let app = Router::new()
    .route("/", get(crate::handlers::index::root))
    .route("/signup", post(|| async { 
        crate::handlers::signup::store_user_info_into_db,
        Redirect::to("/") 
    }));

This code doesn't work, but my idea here is that I want to call store_user_info_into_db function to store user information into a database on form submission. If the form submission was successful, then I want to redirect the user to a home page. Does anyone know how to do this?

1 Answers
async fn store_user_info_into_db() -> Redirect {
    // ...
        
    Redirect::to("/") },
}
Related