Signup tentatively works
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import chalk from 'chalk'
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises';
|
||||
import { pathToFileURL } from 'url';
|
||||
const chalk = require('chalk');
|
||||
const path = require('path');
|
||||
const fs = require('fs/promises');
|
||||
const { pathToFileURL } = require('url');
|
||||
|
||||
function Node(node) {
|
||||
let traits = [
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import dotenv from 'dotenv';
|
||||
import chalk from 'chalk';
|
||||
import jwt from 'jsonwebtoken'
|
||||
import argon2 from 'argon2'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
const dotenv = require("dotenv")
|
||||
const jwt = require('jsonwebtoken');
|
||||
const argon2 = require('argon2');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export default class AuthHandler {
|
||||
|
||||
121
server/db/db.js
121
server/db/db.js
@@ -1,29 +1,21 @@
|
||||
const fs = require('fs/promises');
|
||||
const chalk = require('chalk');
|
||||
const path = require('path');
|
||||
import QuillDB from "../_/quilldb.js"
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import Title from "./model/Title.js"
|
||||
import Member from './model/Member.js'
|
||||
import Token from './model/Token.js'
|
||||
import Titles from "./model/Titles.js"
|
||||
import Members from './model/Members.js'
|
||||
import Tokens from './model/Tokens.js'
|
||||
|
||||
export default class Database {
|
||||
nodes = [];
|
||||
types = [
|
||||
{
|
||||
validate: Title,
|
||||
start: 0,
|
||||
end: null,
|
||||
},
|
||||
{
|
||||
validate: Member,
|
||||
start: null,
|
||||
end: null,
|
||||
},
|
||||
{
|
||||
validate: Token,
|
||||
start: null,
|
||||
end: null,
|
||||
},
|
||||
]
|
||||
titles = new Titles()
|
||||
members = new Members()
|
||||
tokens = new Tokens()
|
||||
|
||||
fromID = {
|
||||
"HY": this.titles,
|
||||
"MEMBER": this.members,
|
||||
"TOKEN": this.tokens
|
||||
}
|
||||
|
||||
constructor() {
|
||||
this.loadData()
|
||||
@@ -38,82 +30,25 @@ export default class Database {
|
||||
dbJson = []
|
||||
}
|
||||
let nodes = dbJson["nodes"];
|
||||
this.validateNodes(nodes)
|
||||
}
|
||||
|
||||
validateNodes(nodes) {
|
||||
nodes = Object.entries(nodes)
|
||||
|
||||
let t = 0
|
||||
|
||||
let currentType = () => {return this.types[t]}
|
||||
let nextType = () => {return this.types[t+1]}
|
||||
let selectNextType = () => {
|
||||
currentType().end = t
|
||||
t += 1;
|
||||
currentType().start = t
|
||||
}
|
||||
let lastNode = (i=null) => {
|
||||
if(i == null) throw new Error("must pass a param to lastNode()")
|
||||
return i+1 === nodes.length
|
||||
}
|
||||
|
||||
for(let i=0; i<nodes.length; i++) {
|
||||
if(this.validateNode(currentType(), nodes[i])) {
|
||||
if(lastNode(i)) {
|
||||
currentType().end = i
|
||||
break;
|
||||
let entries = Object.entries(nodes)
|
||||
|
||||
for(let i=0; i<entries.length; i++) {
|
||||
let entry = entries[i]
|
||||
let id = entry[0]; let node = entry[1];
|
||||
let type = id.split("-")[0]
|
||||
try {
|
||||
let collection = this.fromID[type]
|
||||
if(collection) {
|
||||
collection.add(node)
|
||||
} else {
|
||||
continue;
|
||||
throw new Error("Type does not exist for node: ", id)
|
||||
}
|
||||
} else if(this.validateNode(nextType(), nodes[i])) {
|
||||
selectNextType()
|
||||
continue;
|
||||
} else {
|
||||
throw new Error("Nodes are out of order or corrupted!")
|
||||
} catch(e) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validateNode(type, node) {
|
||||
let [key, value] = node
|
||||
return type.validate(key, value)
|
||||
}
|
||||
|
||||
get = {
|
||||
user: (id) => {
|
||||
return this.nodes[id]
|
||||
},
|
||||
userByEmail: (email) => {
|
||||
for (const id of this.labels["User"]) {
|
||||
const user = this.get.user(id);
|
||||
if (user.email === email) {
|
||||
return { id, ...user }
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
token: (id) => {
|
||||
return this.nodes[`TOKEN-${id}`]
|
||||
}
|
||||
}
|
||||
|
||||
add = {
|
||||
user: (node) => {
|
||||
let lastUser = {}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
submitNewUser(qrCodeID, userInfo) {
|
||||
let newUser = {
|
||||
labels: ["User"],
|
||||
...userInfo
|
||||
}
|
||||
if(User(newUser))
|
||||
this.add.user(newUser)
|
||||
}
|
||||
|
||||
generateUserID() {
|
||||
let id = this.labels["User"].length + 1;
|
||||
while (this.get.user(`user-${id}`)) {
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
export default function Member(id, node) {
|
||||
let idTraits = {
|
||||
firstWord: "MEMBER",
|
||||
length: 2
|
||||
}
|
||||
|
||||
let fields = [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"password"
|
||||
]
|
||||
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split.length === idTraits.length
|
||||
&& split[0] === idTraits.firstWord
|
||||
&& !isNaN(Number(split[1]))
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Member ${node.email} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
64
server/db/model/Members.js
Normal file
64
server/db/model/Members.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
|
||||
export default class Members extends OrderedObject {
|
||||
|
||||
add(newMember) {
|
||||
console.log("adding ", newMember)
|
||||
let id = `MEMBER-${newMember.email}`
|
||||
if(this.validate(id, newMember)) {
|
||||
try {
|
||||
super.add(id, newMember)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
validate(id, node) {
|
||||
let idTraits = {
|
||||
firstWord: "MEMBER"
|
||||
}
|
||||
|
||||
let fields = [
|
||||
"firstName",
|
||||
"lastName",
|
||||
"email",
|
||||
"password"
|
||||
]
|
||||
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split[0] === idTraits.firstWord
|
||||
&& split[1].includes("@")
|
||||
&& split[1].includes(".")
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
console.log("id failed: ", id)
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Member ${node.email} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
console.log("fields failed")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
29
server/db/model/OrderedObject.js
Normal file
29
server/db/model/OrderedObject.js
Normal file
@@ -0,0 +1,29 @@
|
||||
export default class OrderedObject {
|
||||
entries = []
|
||||
ids = {}
|
||||
|
||||
add(id, data) {
|
||||
if(this.get(id)) {
|
||||
console.error(`Can't add item ${id}: id already exists`)
|
||||
throw new global.ServerError(400, `Member with this email already exists`)
|
||||
}
|
||||
this.entries.push(data)
|
||||
this.ids[id] = this.entries.length - 1
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
if (typeof key === "number") {
|
||||
return this.entries[key]
|
||||
} else {
|
||||
return this.entries[this.ids[key]]
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
if (typeof key === "number") {
|
||||
return this.entries[key]
|
||||
} else {
|
||||
return this.entries[this.ids[key]]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
export default function Title(id, node) {
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split.length === 2
|
||||
&& split[0] === "HY"
|
||||
&& !isNaN(Number(split[1]))
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
let fields = [
|
||||
"fullName",
|
||||
]
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Title ${id} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
53
server/db/model/Titles.js
Normal file
53
server/db/model/Titles.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
|
||||
export default class Titles extends OrderedObject {
|
||||
|
||||
add(newTitle) {
|
||||
let id = `HY-${this.entries.length+1}`
|
||||
console.log(id)
|
||||
if(this.validate(id, newTitle)) {
|
||||
try {
|
||||
super.add(id, newTitle)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
validate(id, node) {
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split.length === 2
|
||||
&& split[0] === "HY"
|
||||
&& !isNaN(Number(split[1]))
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
let fields = [
|
||||
"fullName",
|
||||
]
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Title ${id} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
export default function Token(id, node) {
|
||||
let idTraits = {
|
||||
firstWord: "TOKEN"
|
||||
}
|
||||
|
||||
let fields = [
|
||||
"index",
|
||||
"url",
|
||||
"used"
|
||||
]
|
||||
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split[0] === idTraits.firstWord
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Token ${node.email} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
62
server/db/model/Tokens.js
Normal file
62
server/db/model/Tokens.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import OrderedObject from "./OrderedObject.js"
|
||||
|
||||
export default class Tokens extends OrderedObject {
|
||||
|
||||
add(token) {
|
||||
let id = `TOKEN-${token.uuid}`
|
||||
if(this.validate(id, token)) {
|
||||
try {
|
||||
super.add(id, token)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
}
|
||||
|
||||
get(uuid) {
|
||||
return super.get(`TOKEN-${uuid}`)
|
||||
}
|
||||
|
||||
validate(id, node) {
|
||||
let idTraits = {
|
||||
firstWord: "TOKEN"
|
||||
}
|
||||
|
||||
let fields = [
|
||||
"index",
|
||||
"url",
|
||||
"used"
|
||||
]
|
||||
|
||||
let checkID = () => {
|
||||
let split = id.split("-")
|
||||
return (
|
||||
split[0] === idTraits.firstWord
|
||||
)
|
||||
}
|
||||
let idres = checkID()
|
||||
if(!idres) {
|
||||
return false
|
||||
}
|
||||
|
||||
let checkFields = () => {
|
||||
for(let i = 0; i < fields.length; i++) {
|
||||
if(!node[fields[i]]) {
|
||||
throw new Error(`Token ${node.email} is missing trait ${fields[i]}`)
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
let fieldres = checkFields()
|
||||
if(!fieldres) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
116
server/index.js
116
server/index.js
@@ -1,27 +1,22 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import cookieParser from 'cookie-parser'
|
||||
import http from 'http'
|
||||
import fs from 'fs'
|
||||
import chalk from 'chalk'
|
||||
import moment from 'moment'
|
||||
import path from 'path';
|
||||
import { initWebSocket } from './ws.js'
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const cookieParser = require('cookie-parser');
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const chalk = require('chalk');
|
||||
const moment = require('moment');
|
||||
const path = require('path');
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import Database from "./db/db.js"
|
||||
import AuthHandler from './auth.js';
|
||||
import handlers from "./handlers.js";
|
||||
|
||||
// Get __dirname in ES6 environment
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
class Server {
|
||||
db;
|
||||
auth;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
DBPath = path.join(__dirname, '../db')
|
||||
UIPath = path.join(__dirname, './ui')
|
||||
DBPath = path.join(__dirname, './db')
|
||||
|
||||
registerRoutes(router) {
|
||||
// router.post('/api/location', handlers.updateLocation)
|
||||
@@ -39,7 +34,7 @@ class Server {
|
||||
if (!token) {
|
||||
return res.status(400).json({ error: 'Token is required' });
|
||||
}
|
||||
let fromDB = this.db.get.token(token)
|
||||
let fromDB = this.db.tokens.get(token)
|
||||
if (!fromDB) {
|
||||
return res.status(403).json({ error: 'Invalid or expired token' });
|
||||
} else if(fromDB.used) {
|
||||
@@ -50,8 +45,12 @@ class Server {
|
||||
|
||||
newUserSubmission = (req, res) => {
|
||||
const { token } = req.query;
|
||||
db.submitNewUser(token, req.body)
|
||||
return res.status(400).json({ error: 'Haven\t finished this bruh' });
|
||||
try {
|
||||
db.members.add(req.body)
|
||||
return res.redirect(`/signin/?new=true`);
|
||||
} catch(e) {
|
||||
return res.status(e.status).json({ error: 'Error adding new member' });
|
||||
}
|
||||
}
|
||||
|
||||
authMiddleware = (req, res, next) => {
|
||||
@@ -92,23 +91,14 @@ class Server {
|
||||
|
||||
let url = req.url
|
||||
|
||||
let publicPage = () => {
|
||||
url = "/index.html"
|
||||
let filePath = path.join(this.UIPath, "public", url);
|
||||
res.sendFile(filePath, (err) => {
|
||||
if (err) {
|
||||
console.log("File not found, sending fallback:", filePath);
|
||||
res.redirect("/");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let publicFile = () => {
|
||||
let publicSite = () => {
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(this.UIPath, url);
|
||||
} else if(url.includes("75820185")) {
|
||||
filePath = path.join(this.UIPath, "public", url.split("75820185")[1]);
|
||||
} else {
|
||||
filePath = path.join(this.UIPath, "public", url);
|
||||
filePath = path.join(this.UIPath, "public", "index.html");
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
@@ -128,11 +118,7 @@ class Server {
|
||||
}
|
||||
|
||||
if(!this.auth.isLoggedInUser(req, res)) {
|
||||
if(!url.includes(".")) {
|
||||
publicPage()
|
||||
} else {
|
||||
publicFile()
|
||||
}
|
||||
publicSite()
|
||||
} else {
|
||||
privateSite()
|
||||
}
|
||||
@@ -142,10 +128,10 @@ class Server {
|
||||
const formattedDate = moment().format('M.D');
|
||||
const formattedTime = moment().format('h:mma');
|
||||
if(req.url.includes("/api/")) {
|
||||
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
console.logclean(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
} else {
|
||||
if(req.url === "/")
|
||||
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
console.logclean(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
}
|
||||
next();
|
||||
}
|
||||
@@ -154,9 +140,9 @@ class Server {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
if(res.statusCode >= 400) {
|
||||
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
console.logclean(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
} else {
|
||||
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
console.logclean(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
}
|
||||
originalSend.call(this, body);
|
||||
};
|
||||
@@ -184,16 +170,16 @@ class Server {
|
||||
initWebSocket(server);
|
||||
const PORT = 3003;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("*************** Hyperia ***************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
console.logclean("\n")
|
||||
console.logclean(chalk.yellow("*************** Hyperia ***************"))
|
||||
console.logclean(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.logclean(chalk.yellow("***************************************"))
|
||||
console.logclean("\n")
|
||||
});
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log(chalk.red('Closing server...'));
|
||||
console.log(chalk.green('Database connection closed.'));
|
||||
console.logclean(chalk.red('Closing server...'));
|
||||
console.logclean(chalk.green('Database connection closed.'));
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
@@ -201,4 +187,38 @@ class Server {
|
||||
}
|
||||
}
|
||||
|
||||
const _log = console.log;
|
||||
|
||||
console.logclean = function (...args) {
|
||||
_log.call(console, ...args);
|
||||
}
|
||||
|
||||
// console.log = function (...args) {
|
||||
// // Get the caller location
|
||||
// const stack = new Error().stack.split("\n")[2];
|
||||
// const match = stack.match(/(\/.*:\d+:\d+)/);
|
||||
// let location = match ? match[1] : "unknown";
|
||||
|
||||
// // Remove CWD prefix
|
||||
// while (location.startsWith("/")) {
|
||||
// location = location.slice(1);
|
||||
// }
|
||||
// location = "/" + location
|
||||
|
||||
// let cwd = process.cwd();
|
||||
// if (location.startsWith(cwd)) {
|
||||
// location = location.slice(cwd.length);
|
||||
// if (location.startsWith("/")) location = location.slice(1);
|
||||
// }
|
||||
|
||||
// _log.call(console, `[${location}]`, ...args);
|
||||
// };
|
||||
|
||||
global.ServerError = class extends Error {
|
||||
constructor(status, msg) {
|
||||
super(msg);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
const server = new Server()
|
||||
@@ -1,4 +1,4 @@
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
const { WebSocket, WebSocketServer } = require('ws');
|
||||
|
||||
let wss;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user