diff --git a/src/pages/mod.rs b/src/pages/mod.rs index c70cd97f..c7845a76 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -63,7 +63,9 @@ mod tests { PAGES.panel.sitekey.add, PAGES.panel.sitekey.list, PAGES.panel.notifications, - PAGES.panel.settings, + PAGES.panel.settings.home, + PAGES.panel.settings.delete_account, + PAGES.panel.settings.update_secret, &delete_sitekey_url, &edit_sitekey_url, ]; diff --git a/src/pages/panel/mod.rs b/src/pages/panel/mod.rs index 5cdcdd71..7ed5c275 100644 --- a/src/pages/panel/mod.rs +++ b/src/pages/panel/mod.rs @@ -52,18 +52,20 @@ async fn panel(data: AppData, id: Identity) -> PageResult { pub fn services(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(panel); - cfg.service(settings::settings); + settings::services(cfg); sitekey::services(cfg); cfg.service(notifications::notifications); } pub mod routes { + use super::settings::routes::Settings; use super::sitekey::routes::Sitekey; + pub struct Panel { pub home: &'static str, pub sitekey: Sitekey, pub notifications: &'static str, - pub settings: &'static str, + pub settings: Settings, } impl Panel { @@ -72,7 +74,7 @@ pub mod routes { home: "/", sitekey: Sitekey::new(), notifications: "/notifications", - settings: "/settings", + settings: Settings::new(), } } } diff --git a/src/pages/panel/settings.rs b/src/pages/panel/settings.rs index ba095721..36ba5d1b 100644 --- a/src/pages/panel/settings.rs +++ b/src/pages/panel/settings.rs @@ -19,8 +19,35 @@ use actix_web::{HttpResponse, Responder}; use sailfish::TemplateOnce; use crate::errors::PageResult; +use crate::pages::auth::sudo::SudoPage; use crate::AppData; +pub mod routes { + pub struct Settings { + pub home: &'static str, + pub delete_account: &'static str, + pub update_secret: &'static str, + } + + impl Settings { + pub const fn new() -> Self { + Settings { + home: "/settings", + delete_account: "/settings/account/delete", + update_secret: "/settings/secret/update", + } + } + } +} + +pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + cfg.service(settings); + cfg.service(update_secret); + cfg.service(delete_account); +} + +const PAGE: &str = "Settings"; + #[derive(TemplateOnce, Clone)] #[template(path = "panel/settings/index.html")] pub struct IndexPage { @@ -28,10 +55,8 @@ pub struct IndexPage { secret: String, } -const PAGE: &str = "Settings"; - -#[my_codegen::get(path = "crate::PAGES.panel.settings", wrap = "crate::CheckLogin")] -pub async fn settings(data: AppData, id: Identity) -> PageResult { +#[my_codegen::get(path = "crate::PAGES.panel.settings.home", wrap = "crate::CheckLogin")] +async fn settings(data: AppData, id: Identity) -> PageResult { let username = id.identity().unwrap(); let details = sqlx::query_as!( @@ -47,3 +72,29 @@ pub async fn settings(data: AppData, id: Identity) -> PageResult .content_type("text/html; charset=utf-8") .body(body)) } + +#[my_codegen::get( + path = "crate::PAGES.panel.settings.delete_account", + wrap = "crate::CheckLogin" +)] +async fn delete_account() -> impl Responder { + let page = SudoPage::::new(crate::V1_API_ROUTES.account.delete, None) + .render_once() + .unwrap(); + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(&page) +} + +#[my_codegen::get( + path = "crate::PAGES.panel.settings.update_secret", + wrap = "crate::CheckLogin" +)] +async fn update_secret() -> impl Responder { + let page = SudoPage::::new(crate::V1_API_ROUTES.account.update_secret, None) + .render_once() + .unwrap(); + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(&page) +} diff --git a/templates/auth/register/ts/emailExists.ts b/templates/auth/register/ts/emailExists.ts index 11fd5e89..cb714b7f 100644 --- a/templates/auth/register/ts/emailExists.ts +++ b/templates/auth/register/ts/emailExists.ts @@ -20,9 +20,15 @@ import ROUTES from '../../../api/v1/routes'; import genJsonPayload from '../../../utils/genJsonPayload'; import createError from '../../../components/error/index'; -const emailExists = async () => { - const email = document.getElementById('email'); +const emailExists = async (element?: HTMLInputElement) => { + let email; + if (element === undefined || element === null) { + email = document.getElementById('email'); + } else { + email = element; + } const val = email.value; + const payload = { val, }; diff --git a/templates/auth/sudo/index.ts b/templates/auth/sudo/index.ts index bff7a0af..ce8b3109 100644 --- a/templates/auth/sudo/index.ts +++ b/templates/auth/sudo/index.ts @@ -14,21 +14,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +import LazyElement from '../../utils/lazyElement'; -const form = () => { - let element = null; - const ID = 'form'; +const ID = 'form'; +const FORM = new LazyElement(ID); - if (element === null) { - element = document.getElementById(ID); - if (element === undefined) { - throw new Error("Couldn't form element, is the component loaded?"); - } else { - return element; - } - } else { - element; - } -}; - -export default form; +export default FORM; diff --git a/templates/index.ts b/templates/index.ts index 220facba..ecd82520 100644 --- a/templates/index.ts +++ b/templates/index.ts @@ -21,6 +21,8 @@ import * as login from './auth/login/ts/'; import * as register from './auth/register/ts/'; import * as panel from './panel/ts/index'; import settings from './panel/settings/'; +import * as deleteAccount from './panel/settings/account/delete'; +import * as updateSecret from './panel/settings/secret/update'; import * as addSiteKey from './panel/sitekey/add/ts'; import * as editSitekey from './panel/sitekey/edit/'; import * as deleteSitekey from './panel/sitekey/delete/'; @@ -53,6 +55,8 @@ const router = new Router(); router.register(VIEWS.panelHome, panel.index); router.register(VIEWS.settings, settings); +router.register(VIEWS.deleteAccount, deleteAccount.index); +router.register(VIEWS.updateSecret, updateSecret.index); router.register(VIEWS.registerUser, register.index); router.register(VIEWS.loginUser, login.index); router.register(VIEWS.notifications, notidications.index); diff --git a/templates/panel/navbar/index.html b/templates/panel/navbar/index.html index ab49c486..764e698d 100644 --- a/templates/panel/navbar/index.html +++ b/templates/panel/navbar/index.html @@ -35,7 +35,7 @@
  • - + " alt="" />
    Settings diff --git a/templates/panel/settings/account/delete.ts b/templates/panel/settings/account/delete.ts new file mode 100644 index 00000000..e93842ac --- /dev/null +++ b/templates/panel/settings/account/delete.ts @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import {getPassword} from '../../../auth/login/ts/'; +import FORM from '../../../auth/sudo/'; + +import getFormUrl from '../../../utils/getFormUrl'; +import genJsonPayload from '../../../utils/genJsonPayload'; +import createError from '../../../components/error'; + +import VIEWS from '../../../views/v1/routes'; + +const submit = async (e: Event) => { + e.preventDefault(); + const password = getPassword(); + + const payload = { + password, + }; + + const formUrl = getFormUrl(FORM.get()); + + const res = await fetch(formUrl, genJsonPayload(payload)); + if (res.ok) { + window.location.assign(VIEWS.panelHome); + } else { + const err = await res.json(); + createError(err.error); + } +}; + +export const index = () => { + FORM.get().addEventListener('submit', submit, true); +}; diff --git a/templates/panel/settings/index.html b/templates/panel/settings/index.html index 5d38306e..6ac3af4b 100644 --- a/templates/panel/settings/index.html +++ b/templates/panel/settings/index.html @@ -17,7 +17,7 @@

    <.= PAGE .>

    -