diff --git a/examples/user_management.rs b/examples/user_management.rs index 053e741..3ff937e 100644 --- a/examples/user_management.rs +++ b/examples/user_management.rs @@ -35,7 +35,7 @@ async fn main() -> Result<()> { .add_um_routes::() // Start the server - .serve_unix("localhost:1965") + .serve_ip("localhost:1965") .await } diff --git a/src/user_management/routes.rs b/src/user_management/routes.rs index 195f707..008c4af 100644 --- a/src/user_management/routes.rs +++ b/src/user_management/routes.rs @@ -29,6 +29,7 @@ pub trait UserManagementRoutes: private::Sealed { /// * `/account/register`, for users to register a new account /// * `/account/login`, for users to link their certificate to an existing account /// * `/account/password`, to change the user's password + /// * `/account/delete`, to delete an account /// /// If this method is used, no more routes should be added under `/account`. If you /// would like to direct a user to login from your application, you should send them @@ -36,7 +37,7 @@ pub trait UserManagementRoutes: private::Sealed { /// /// The `redir` argument allows you to specify the point that users will be directed /// to return to once their account has been created. - fn add_um_routes(self) -> Self; + fn add_um_routes(self) -> Self; /// Add a special route that requires users to be logged in /// @@ -93,14 +94,15 @@ impl UserManagementRoutes for crate::Server { /// Add pre-configured routes to the serve to handle authentication /// /// See [`UserManagementRoutes::add_um_routes()`] - fn add_um_routes(self) -> Self { + fn add_um_routes(self) -> Self { let clients_page = Response::success_gemini(include_str!("pages/clients.gmi")); #[allow(unused_mut)] let mut modified_self = self.add_route("/account", handle_base::) .add_route("/account/askcert", handle_ask_cert::) .add_route("/account/register", handle_register::) - .add_route("/account/clients", clients_page); + .add_route("/account/clients", clients_page) + .add_authenticated_route("/account/delete", handle_delete::); #[cfg(feature = "user_management_advanced")] { modified_self = modified_self @@ -279,6 +281,29 @@ async fn handle_register(reque }) } +async fn handle_delete( + request: Request, + user: RegisteredUser, +) -> Result { + Ok(match request.input() { + Some(username) if user.username() == username => { + user.delete()?; + Response::success_gemini(include_str!("pages/deleted.gmi")) + }, + Some(_) => { + Response::bad_request("That username did not match. Your account has not been deleted.") + }, + None => { + Response::input( + format!( + "Are you sure you'd like to delete your account? Please enter your username, \"{}\", to continue", + user.username(), + ) + ) + }, + }) +} + #[cfg(feature = "user_management_advanced")] async fn handle_login(request: Request) -> Result { Ok(match request.user::()? {