#
mcaptcha/api/v1/account/
delete.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6use actix_identity::Identity;
    7use actix_web::{web, HttpResponse, Responder};
    8
    9use super::auth::runners::Password;
    10use crate::errors::*;
    11use crate::AppData;
    12
    13#[my_codegen::post(
    14    path = "crate::V1_API_ROUTES.account.delete",
    15    wrap = "crate::api::v1::get_middleware()"
    16)]
    17pub async fn delete_account(
    18    id: Identity,
    19    payload: web::Json<Password>,
    20    data: AppData,
    21) -> ServiceResult<impl Responder> {
    22    use argon2_creds::Config;
    23
    24    let username = id.identity().unwrap();
    25
    26    let hash = data
    27        .db
    28        .get_password(&db_core::Login::Username(&username))
    29        .await?;
    30
    31    if Config::verify(&hash.hash, &payload.password)? {
    32        runners::delete_user(&username, &data).await?;
    33        id.forget();
    34        Ok(HttpResponse::Ok())
    35    } else {
    36        Err(ServiceError::WrongPassword)
    37    }
    38}
    39
    40pub mod runners {
    41
    42    use super::*;
    43
    44    pub async fn delete_user(name: &str, data: &AppData) -> ServiceResult<()> {
    45        data.db.delete_user(name).await?;
    46        Ok(())
    47    }
    48}
    49
    50pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
    51    cfg.service(delete_account);
    52}