mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-14 13:08:27 +03:00
lazy element, settings: account delete and secret update
This commit is contained in:
parent
2c2f79e1cd
commit
861998af75
15 changed files with 308 additions and 36 deletions
|
@ -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,
|
||||
];
|
||||
|
|
|
@ -52,18 +52,20 @@ async fn panel(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
|||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<impl Responder> {
|
||||
#[my_codegen::get(path = "crate::PAGES.panel.settings.home", wrap = "crate::CheckLogin")]
|
||||
async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder> {
|
||||
let username = id.identity().unwrap();
|
||||
|
||||
let details = sqlx::query_as!(
|
||||
|
@ -47,3 +72,29 @@ pub async fn settings(data: AppData, id: Identity) -> PageResult<impl Responder>
|
|||
.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::<u8, u8>::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::<u8, u8>::new(crate::V1_API_ROUTES.account.update_secret, None)
|
||||
.render_once()
|
||||
.unwrap();
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(&page)
|
||||
}
|
||||
|
|
|
@ -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 = <HTMLInputElement>document.getElementById('email');
|
||||
const emailExists = async (element?: HTMLInputElement) => {
|
||||
let email;
|
||||
if (element === undefined || element === null) {
|
||||
email = <HTMLInputElement>document.getElementById('email');
|
||||
} else {
|
||||
email = element;
|
||||
}
|
||||
const val = email.value;
|
||||
|
||||
const payload = {
|
||||
val,
|
||||
};
|
||||
|
|
|
@ -14,21 +14,9 @@
|
|||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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 = <HTMLFormElement>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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
</li>
|
||||
|
||||
<li class="secondary-menu__item">
|
||||
<a class="secondary-menu__item-link" href="<.= crate::PAGES.panel.settings .>">
|
||||
<a class="secondary-menu__item-link" href="<.= crate::PAGES.panel.settings.home .>">
|
||||
<img class="secondary-menu__icon" src="<.= crate::FILES.get("./static/cache/img/svg/settings.svg").unwrap() .>" alt="" />
|
||||
<div class="secondary-menu__item-name">
|
||||
Settings
|
||||
|
|
48
templates/panel/settings/account/delete.ts
Normal file
48
templates/panel/settings/account/delete.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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(<HTMLFormElement>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);
|
||||
};
|
|
@ -17,7 +17,7 @@
|
|||
<h1 class="form__title">
|
||||
<.= PAGE .>
|
||||
</h1>
|
||||
<form class="settings__form "
|
||||
<form class="settings__form" id="settings__email-form"
|
||||
action="<.= crate::V1_API_ROUTES.account.update_email .>"
|
||||
method="post">
|
||||
<label class="settings-form__label" for="email">
|
||||
|
@ -38,7 +38,7 @@
|
|||
</form>
|
||||
|
||||
<form
|
||||
class="settings__form"
|
||||
class="settings__form" id="settings__secret-form"
|
||||
action="<.= crate::V1_API_ROUTES.account.update_secret .>"
|
||||
method="post">
|
||||
<label class="settings-form__label" for="secret">
|
||||
|
@ -67,7 +67,7 @@
|
|||
</form>
|
||||
|
||||
<form
|
||||
class="settings__form"
|
||||
class="settings__form" id="settings__delete-form"
|
||||
action="<.= crate::V1_API_ROUTES.account.update_secret .>"
|
||||
method="post">
|
||||
<label class="settings-form__label--danger" for="delete-account">
|
||||
|
|
|
@ -17,13 +17,87 @@
|
|||
|
||||
import registerShowPassword from '../../components/showPassword/';
|
||||
import CopyIcon from '../../components/clipboard/';
|
||||
import createError from '../../components/error/';
|
||||
|
||||
import emailExists from '../../auth/register/ts/emailExists';
|
||||
|
||||
import LazyElement from '../../utils/lazyElement';
|
||||
import isBlankString from '../../utils/isBlankString';
|
||||
import getFormUrl from '../../utils/getFormUrl';
|
||||
import genJsonPayload from '../../utils/genJsonPayload';
|
||||
|
||||
import VIEWS from '../../views/v1/routes';
|
||||
|
||||
const SECRET_COPY_ICON = 'settings__secret-copy';
|
||||
const SECRET_COPY_DONE_ICON = 'settings__secret-copy-done';
|
||||
|
||||
const index = () => {
|
||||
registerShowPassword();
|
||||
// form IDs
|
||||
const DELETE_FORM = 'settings__delete-form';
|
||||
const EMAIL_FORM = 'settings__email-form';
|
||||
const SECRET_FORM = 'settings__secret-form';
|
||||
|
||||
// form elements
|
||||
const deleteForm = new LazyElement(DELETE_FORM);
|
||||
const emailForm = new LazyElement(EMAIL_FORM);
|
||||
const secretForm = new LazyElement(SECRET_FORM);
|
||||
|
||||
// field IDs
|
||||
const EMAIL = 'email';
|
||||
|
||||
// field elements
|
||||
const emailField = new LazyElement(EMAIL);
|
||||
|
||||
// form event handlers
|
||||
const updateEmail = async (e: Event) => {
|
||||
e.preventDefault();
|
||||
const emailElement = <HTMLInputElement>emailField.get();
|
||||
const email = emailElement.value;
|
||||
isBlankString(email, 'email', e);
|
||||
if (await emailExists(emailElement)) {
|
||||
return;
|
||||
} else {
|
||||
const url = getFormUrl(<HTMLFormElement>emailForm.get());
|
||||
const payload = {
|
||||
email,
|
||||
};
|
||||
|
||||
const res = await fetch(url, genJsonPayload(payload));
|
||||
if (res.ok) {
|
||||
window.location.reload();
|
||||
} else {
|
||||
const err = await res.json();
|
||||
createError(err.error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const updateSecret = (e: Event) => {
|
||||
e.preventDefault();
|
||||
const msg =
|
||||
'WARNING: updating secret will cause service disruption if old secret is still in use post update';
|
||||
if (confirm(msg)) {
|
||||
window.location.assign(VIEWS.updateSecret);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteAccount = (e: Event) => {
|
||||
e.preventDefault();
|
||||
const msg =
|
||||
"WARNING: all CAPTCHA configurations will be lost. This action can't be undone";
|
||||
if (confirm(msg)) {
|
||||
window.location.assign(VIEWS.deleteAccount);
|
||||
}
|
||||
};
|
||||
|
||||
// regist form event handlers
|
||||
const registerForms = () => {
|
||||
deleteForm.get().addEventListener('submit', e => deleteAccount(e), true);
|
||||
emailForm.get().addEventListener('submit', e => updateEmail(e), true);
|
||||
secretForm.get().addEventListener('submit', e => updateSecret(e), true);
|
||||
};
|
||||
|
||||
// set up copying account secret to clipboard
|
||||
const initCopySecret = () => {
|
||||
const secretElement = <HTMLElement>(
|
||||
document.querySelector(`.${SECRET_COPY_ICON}`)
|
||||
);
|
||||
|
@ -31,4 +105,12 @@ const index = () => {
|
|||
new CopyIcon(writeText, secretElement, SECRET_COPY_DONE_ICON);
|
||||
};
|
||||
|
||||
/// TODO email update button should only change if email value has been changed
|
||||
|
||||
const index = () => {
|
||||
registerShowPassword();
|
||||
initCopySecret();
|
||||
registerForms();
|
||||
};
|
||||
|
||||
export default index;
|
||||
|
|
|
@ -97,7 +97,8 @@
|
|||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.settings__submit-btn:hover {
|
||||
.settings__submit-btn:hover,
|
||||
.settings__submit-btn--danger:hover {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
|
|
48
templates/panel/settings/secret/update.ts
Normal file
48
templates/panel/settings/secret/update.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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(<HTMLFormElement>FORM.get());
|
||||
|
||||
const res = await fetch(formUrl, genJsonPayload(payload));
|
||||
if (res.ok) {
|
||||
window.location.assign(VIEWS.settings);
|
||||
} else {
|
||||
const err = await res.json();
|
||||
createError(err.error);
|
||||
}
|
||||
};
|
||||
|
||||
export const index = () => {
|
||||
FORM.get().addEventListener('submit', submit, true);
|
||||
};
|
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
|
||||
import {getPassword} from '../../../auth/login/ts/';
|
||||
import form from '../../../auth/sudo/';
|
||||
import FORM from '../../../auth/sudo/';
|
||||
import additionalData from '../../../components/additional-data';
|
||||
|
||||
import getFormUrl from '../../../utils/getFormUrl';
|
||||
|
@ -35,7 +35,7 @@ const submit = async (e: Event) => {
|
|||
key,
|
||||
};
|
||||
|
||||
const formUrl = getFormUrl(form());
|
||||
const formUrl = getFormUrl(<HTMLFormElement>FORM.get());
|
||||
|
||||
const res = await fetch(formUrl, genJsonPayload(payload));
|
||||
if (res.ok) {
|
||||
|
@ -47,5 +47,5 @@ const submit = async (e: Event) => {
|
|||
};
|
||||
|
||||
export const index = () => {
|
||||
form().addEventListener('submit', submit, true);
|
||||
FORM.get().addEventListener('submit', submit, true);
|
||||
};
|
||||
|
|
38
templates/utils/lazyElement.ts
Normal file
38
templates/utils/lazyElement.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
class LazyElement {
|
||||
id: string;
|
||||
element: HTMLElement;
|
||||
constructor(id: string) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
get() {
|
||||
if (this.element === null || this.element === undefined) {
|
||||
const element = document.getElementById(this.id);
|
||||
if (element === null || element === undefined) {
|
||||
throw new Error(`Element ${this.id} is undefined`);
|
||||
} else {
|
||||
this.element = element;
|
||||
}
|
||||
}
|
||||
return this.element;
|
||||
}
|
||||
}
|
||||
|
||||
export default LazyElement;
|
|
@ -21,6 +21,8 @@ const ROUTES = {
|
|||
signoutUser: '/api/v1/signout',
|
||||
panelHome: '/',
|
||||
settings: '/settings/',
|
||||
updateSecret: '/settings/secret/update/',
|
||||
deleteAccount: '/settings/account/delete/',
|
||||
docsHome: '/docs/',
|
||||
notifications: '/notifications',
|
||||
listSitekey: '/sitekeys/',
|
||||
|
|
Loading…
Add table
Reference in a new issue