Browse Source

Added a route to delete the user's account

devel
Emi Tatsuo 2 years ago
parent
commit
65fe416973
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
  1. 2
      examples/user_management.rs
  2. 31
      src/user_management/routes.rs

2
examples/user_management.rs

@ -35,7 +35,7 @@ async fn main() -> Result<()> {
.add_um_routes::<String>()
// Start the server
.serve_unix("localhost:1965")
.serve_ip("localhost:1965")
.await
}

31
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<UserData: Serialize + DeserializeOwned + Default + 'static>(self) -> Self;
fn add_um_routes<UserData: Serialize + DeserializeOwned + Default + Send + Sync + 'static>(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<UserData: Serialize + DeserializeOwned + Default + 'static>(self) -> Self {
fn add_um_routes<UserData: Serialize + DeserializeOwned + Default + Send + Sync + 'static>(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::<UserData>)
.add_route("/account/askcert", handle_ask_cert::<UserData>)
.add_route("/account/register", handle_register::<UserData>)
.add_route("/account/clients", clients_page);
.add_route("/account/clients", clients_page)
.add_authenticated_route("/account/delete", handle_delete::<UserData>);
#[cfg(feature = "user_management_advanced")] {
modified_self = modified_self
@ -279,6 +281,29 @@ async fn handle_register<UserData: Serialize + DeserializeOwned + Default>(reque
})
}
async fn handle_delete<UserData: Serialize + DeserializeOwned + Sync + Send>(
request: Request,
user: RegisteredUser<UserData>,
) -> Result<Response> {
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<UserData: Serialize + DeserializeOwned + Default>(request: Request) -> Result<Response> {
Ok(match request.user::<UserData>()? {

Loading…
Cancel
Save