#
mcaptcha/api/v1/account/
email.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};
    8use db_core::UpdateEmail;
    9use serde::{Deserialize, Serialize};
    10
    11use super::{AccountCheckPayload, AccountCheckResp};
    12use crate::errors::*;
    13use crate::AppData;
    14
    15#[derive(Clone, Debug, Deserialize, Serialize)]
    16pub struct Email {
    17    pub email: String,
    18}
    19
    20#[my_codegen::post(path = "crate::V1_API_ROUTES.account.email_exists")]
    21pub async fn email_exists(
    22    payload: web::Json<AccountCheckPayload>,
    23    data: AppData,
    24) -> ServiceResult<impl Responder> {
    25    let exists = data.db.email_exists(&payload.val).await?;
    26
    27    let resp = AccountCheckResp { exists };
    28
    29    Ok(HttpResponse::Ok().json(resp))
    30}
    31
    32/// update email
    33#[my_codegen::post(
    34    path = "crate::V1_API_ROUTES.account.update_email",
    35    wrap = "crate::api::v1::get_middleware()"
    36)]
    37async fn set_email(
    38    id: Identity,
    39    payload: web::Json<Email>,
    40    data: AppData,
    41) -> ServiceResult<impl Responder> {
    42    let username = id.identity().unwrap();
    43
    44    data.creds.email(&payload.email)?;
    45
    46    let update_email = UpdateEmail {
    47        username: &username,
    48        new_email: &payload.email,
    49    };
    50
    51    data.db.update_email(&update_email).await?;
    52
    53    Ok(HttpResponse::Ok())
    54}
    55
    56pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
    57    cfg.service(email_exists);
    58    cfg.service(set_email);
    59}