add captcha

This commit is contained in:
biglyderv 2024-11-25 14:12:44 -05:00
parent 3dea44dacf
commit edbadbcf2e
12 changed files with 115 additions and 7 deletions

26
form/captcha.js Normal file
View file

@ -0,0 +1,26 @@
import Route from "../route.js";
import initDb from "../db.js";
let db = await initDb();
let main = new Route([], async function (req, res, input) {
let body = { ...req.cookies, ...req.body };
let { captcha, challenger } = body;
let match = await db.all('SELECT * FROM captcha WHERE key = ? AND solution = ?', [
captcha,
challenger
]);
await db.all('DELETE FROM captcha WHERE key = ? AND solution = ?', [
captcha,
challenger
]);
return {
captchaMatch: (match.length > 0)
};
});
export default main;

View file

@ -2,11 +2,16 @@ import Route from "../route.js";
import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import auth from "../form/auth.js";
import captcha from "./captcha.js";
let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let main = new Route([auth,captcha], async function (req, res, input) {
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { username } = input;
let id = randomUUID();

View file

@ -3,12 +3,16 @@ import Route from "../route.js";
import initDb from "../db.js";
import { compare } from "bcrypt";
import { randomBytes } from 'node:crypto';
import captcha from "./captcha.js";
let db = await initDb();
// TODO: rewrite
let main = new Route([], async function (req, res, input) {
let main = new Route([captcha], async function (req, res, input) {
let { user, pass } = req.body;
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
if (!pass || !user) return { 'success': false, 'message': 'Some fields are missing' };

View file

@ -2,6 +2,7 @@
import Route from "../route.js";
import initDb from "../db.js";
import { hash } from "bcrypt";
import captcha from "./captcha.js";
const minChar = 1;
const maxChar = 32;
@ -13,8 +14,11 @@ function isValid(user) {
}
// TODO: rewrite
let main = new Route([], async function (req, res, input) {
let main = new Route([captcha], async function (req, res, input) {
let { user, pass, pass2 } = req.body;
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
if (pass != pass2) return { 'success': false, 'message': 'Passwords do not match' };

View file

@ -3,13 +3,17 @@ import initDb from "../db.js";
import auth from "../form/auth.js";
import {exec} from 'node:child_process';
import { promisify } from "node:util";
import captcha from "./captcha.js";
const execP = promisify(exec);
let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let main = new Route([auth,captcha], async function (req, res, input) {
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { path } = req.file;
let { username } = input;

View file

@ -4,13 +4,17 @@ import { randomUUID } from 'node:crypto';
import {exec} from 'node:child_process';
import { promisify } from "node:util";
import auth from "../form/auth.js";
import captcha from "./captcha.js";
const execP = promisify(exec);
let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let main = new Route([auth,captcha], async function (req, res, input) {
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { username } = input;
let id = randomUUID();