mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
error handling in auth
This commit is contained in:
@@ -104,6 +104,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
|
|
||||||
let data = Data::new().await;
|
let data = Data::new().await;
|
||||||
sqlx::migrate!("./migrations/").run(&data.db).await.unwrap();
|
sqlx::migrate!("./migrations/").run(&data.db).await.unwrap();
|
||||||
|
|
||||||
|
println!("Starting server on: http://{}", SETTINGS.server.get_ip());
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(actix_middleware::Logger::default())
|
.wrap(actix_middleware::Logger::default())
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ const PAGE: &str = "Login";
|
|||||||
|
|
||||||
impl Default for IndexPage {
|
impl Default for IndexPage {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
IndexPage }
|
IndexPage
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import isBlankString from '../../../utils/isBlankString';
|
|||||||
import genJsonPayload from '../../../utils/genJsonPayload';
|
import genJsonPayload from '../../../utils/genJsonPayload';
|
||||||
import getFormUrl from '../../../utils/getFormUrl';
|
import getFormUrl from '../../../utils/getFormUrl';
|
||||||
import registerShowPassword from '../../../components/showPassword';
|
import registerShowPassword from '../../../components/showPassword';
|
||||||
|
import createError from '../../../components/error/index';
|
||||||
|
|
||||||
//import '../forms.scss';
|
//import '../forms.scss';
|
||||||
|
|
||||||
@@ -52,11 +53,10 @@ const login = async (e: Event) => {
|
|||||||
|
|
||||||
const res = await fetch(formUrl, genJsonPayload(payload));
|
const res = await fetch(formUrl, genJsonPayload(payload));
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
alert('success');
|
|
||||||
window.location.assign(VIEWS.panelHome);
|
window.location.assign(VIEWS.panelHome);
|
||||||
} else {
|
} else {
|
||||||
const err = await res.json();
|
const err = await res.json();
|
||||||
alert(`error: ${err.error}`);
|
createError(err.error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import emailExists from './emailExists';
|
|||||||
|
|
||||||
import {mockAlert, getRegistrationFormHtml} from '../../../setUpTests';
|
import {mockAlert, getRegistrationFormHtml} from '../../../setUpTests';
|
||||||
|
|
||||||
|
import setup from '../../../components/error/setUpTests';
|
||||||
|
|
||||||
fetchMock.enableMocks();
|
fetchMock.enableMocks();
|
||||||
mockAlert();
|
mockAlert();
|
||||||
|
|
||||||
@@ -32,6 +34,8 @@ it('finds exchange', async () => {
|
|||||||
fetchMock.mockResponseOnce(JSON.stringify({exists: true}));
|
fetchMock.mockResponseOnce(JSON.stringify({exists: true}));
|
||||||
|
|
||||||
document.body.innerHTML = getRegistrationFormHtml();
|
document.body.innerHTML = getRegistrationFormHtml();
|
||||||
|
document.querySelector('body').appendChild(setup());
|
||||||
|
|
||||||
const emailField = <HTMLInputElement>document.getElementById('email');
|
const emailField = <HTMLInputElement>document.getElementById('email');
|
||||||
emailField.setAttribute('value', 'test@a.com');
|
emailField.setAttribute('value', 'test@a.com');
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
import ROUTES from '../../../api/v1/routes';
|
import ROUTES from '../../../api/v1/routes';
|
||||||
|
|
||||||
import genJsonPayload from '../../../utils/genJsonPayload';
|
import genJsonPayload from '../../../utils/genJsonPayload';
|
||||||
|
import createError from '../../../components/error/index';
|
||||||
|
|
||||||
const emailExists = async () => {
|
const emailExists = async () => {
|
||||||
const email = <HTMLInputElement>document.getElementById('email');
|
const email = <HTMLInputElement>document.getElementById('email');
|
||||||
@@ -31,13 +32,13 @@ const emailExists = async () => {
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.exists) {
|
if (data.exists) {
|
||||||
email.className += ' form__in-field--warn';
|
email.className += ' form__in-field--warn';
|
||||||
alert('Email taken');
|
createError(`Email "${val}" is already used`);
|
||||||
return data.exists;
|
return data.exists;
|
||||||
}
|
}
|
||||||
return data.exists;
|
return data.exists;
|
||||||
} else {
|
} else {
|
||||||
const err = await res.json();
|
const err = await res.json();
|
||||||
alert(`error: ${err.error}`);
|
createError(err.error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import userExists from './userExists';
|
|||||||
import emailExists from './emailExists';
|
import emailExists from './emailExists';
|
||||||
import getFormUrl from '../../../utils/getFormUrl';
|
import getFormUrl from '../../../utils/getFormUrl';
|
||||||
import registerShowPassword from '../../../components/showPassword';
|
import registerShowPassword from '../../../components/showPassword';
|
||||||
|
import createError from '../../../components/error/index';
|
||||||
|
|
||||||
//import '../forms.scss';
|
//import '../forms.scss';
|
||||||
|
|
||||||
@@ -72,11 +73,10 @@ const registerUser = async (e: Event) => {
|
|||||||
|
|
||||||
const res = await fetch(formUrl, genJsonPayload(payload));
|
const res = await fetch(formUrl, genJsonPayload(payload));
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
alert('success');
|
|
||||||
window.location.assign(VIEWS.loginUser);
|
window.location.assign(VIEWS.loginUser);
|
||||||
} else {
|
} else {
|
||||||
const err = await res.json();
|
const err = await res.json();
|
||||||
alert(`error: ${err.error}`);
|
createError(err.error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ import userExists from './userExists';
|
|||||||
|
|
||||||
import {mockAlert, getLoginFormHtml} from '../../../setUpTests';
|
import {mockAlert, getLoginFormHtml} from '../../../setUpTests';
|
||||||
|
|
||||||
|
import setup from '../../../components/error/setUpTests';
|
||||||
|
|
||||||
fetchMock.enableMocks();
|
fetchMock.enableMocks();
|
||||||
mockAlert();
|
mockAlert();
|
||||||
|
|
||||||
@@ -32,6 +34,7 @@ it('finds exchange', async () => {
|
|||||||
fetchMock.mockResponseOnce(JSON.stringify({exists: true}));
|
fetchMock.mockResponseOnce(JSON.stringify({exists: true}));
|
||||||
|
|
||||||
document.body.innerHTML = getLoginFormHtml();
|
document.body.innerHTML = getLoginFormHtml();
|
||||||
|
document.querySelector('body').appendChild(setup());
|
||||||
const usernameField = <HTMLInputElement>document.querySelector('#username');
|
const usernameField = <HTMLInputElement>document.querySelector('#username');
|
||||||
usernameField.value = 'test';
|
usernameField.value = 'test';
|
||||||
expect(await userExists()).toBe(true);
|
expect(await userExists()).toBe(true);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
import ROUTES from '../../../api/v1/routes';
|
import ROUTES from '../../../api/v1/routes';
|
||||||
|
|
||||||
import genJsonPayload from '../../../utils/genJsonPayload';
|
import genJsonPayload from '../../../utils/genJsonPayload';
|
||||||
|
import createError from '../../../components/error/index';
|
||||||
|
|
||||||
const userExists = async () => {
|
const userExists = async () => {
|
||||||
const username = <HTMLInputElement>document.getElementById('username');
|
const username = <HTMLInputElement>document.getElementById('username');
|
||||||
@@ -31,12 +32,12 @@ const userExists = async () => {
|
|||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
if (data.exists) {
|
if (data.exists) {
|
||||||
username.className += ' form__in-field--warn';
|
username.className += ' form__in-field--warn';
|
||||||
alert('Username taken');
|
createError(`Username "${val}" taken`);
|
||||||
}
|
}
|
||||||
return data.exists;
|
return data.exists;
|
||||||
} else {
|
} else {
|
||||||
const err = await res.json();
|
const err = await res.json();
|
||||||
alert(`error: ${err.error}`);
|
createError(err.error);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -18,16 +18,12 @@
|
|||||||
import createError from './index';
|
import createError from './index';
|
||||||
import * as e from './index';
|
import * as e from './index';
|
||||||
|
|
||||||
|
import setup from './setUpTests';
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const setup = () => {
|
|
||||||
let x = document.createElement('div');
|
|
||||||
x.id = e.ERR_CONTAINER_ID;
|
|
||||||
return x;
|
|
||||||
};
|
|
||||||
|
|
||||||
it('checks if error boxes work', () => {
|
it('checks if error boxes work', () => {
|
||||||
document.body.append(setup());
|
document.body.append(setup());
|
||||||
|
|
||||||
|
|||||||
1
templates/components/error/index.html
Normal file
1
templates/components/error/index.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<div id="err__container"></div>
|
||||||
25
templates/components/error/setUpTests.ts
Normal file
25
templates/components/error/setUpTests.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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 * as e from './index';
|
||||||
|
|
||||||
|
const setup = () => {
|
||||||
|
let x = document.createElement('div');
|
||||||
|
x.id = e.ERR_CONTAINER_ID;
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default setup;
|
||||||
@@ -15,5 +15,6 @@
|
|||||||
href="<.= &*crate::MOBILE_CSS .>"
|
href="<.= &*crate::MOBILE_CSS .>"
|
||||||
/>
|
/>
|
||||||
<script src="<.= &*crate::JS .>"></script>
|
<script src="<.= &*crate::JS .>"></script>
|
||||||
|
<. include!("../components/error/index.html"); .>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user