User Submitting Signup

This commit is contained in:
metacryst
2025-11-18 07:58:35 -06:00
parent 81ca35bf2f
commit b8e48f7945
8 changed files with 196 additions and 35 deletions

View File

@@ -1,6 +1,20 @@
import QuillDB from "../_/quilldb.js"
import fs from 'fs/promises'
import path from 'path'
export default class Database extends QuillDB {
tokens;
constructor() {
super()
this.loadTokens()
}
async loadTokens() {
const tokenData = await fs.readFile(path.join(process.cwd(), 'db/tokens.json'), 'utf8');
let tokenJSON = JSON.parse(tokenData);
this.tokens = tokenJSON
}
get = {
user: (id) => {
@@ -15,6 +29,9 @@ export default class Database extends QuillDB {
}
return null;
},
token: (id) => {
return this.tokens[id]
}
}
generateUserID() {

View File

@@ -27,11 +27,31 @@ class Server {
// router.post('/api/location', handlers.updateLocation)
router.post('/login', this.auth.login)
router.get('/signout', this.auth.logout)
router.get('/signup', this.verifyToken, this.get)
router.post('/signup', this.verifyToken, this.newUserSubmission)
router.get('/db/images/*', this.getUserImage)
router.get('/*', this.get)
return router
}
verifyToken = (req, res, next) => {
const { token } = req.query;
if (!token) {
return res.status(400).json({ error: 'Token is required' });
}
let fromDB = this.db.get.token(token)
if (!fromDB) {
return res.status(403).json({ error: 'Invalid or expired token' });
} else if(fromDB.used) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
next()
}
newUserSubmission = (req, res) => {
return res.status(400).json({ error: 'Haven\t finished this bruh' });
}
authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
@@ -67,37 +87,32 @@ class Server {
}
get = async (req, res) => {
if(!this.auth.isLoggedInUser(req, res)) {
console.log("Not logged in")
let url = req.url
if(!url.includes(".")) { // Page request
if(url === "/") {
url = "/index.html"
} else {
url = path.join("/pages", url) + ".html"
}
let url = req.url
let filePath = path.join(this.UIPath, "public", url);
res.sendFile(filePath, (err) => {
if (err) {
console.log("File not found, sending fallback:", filePath);
res.redirect("/");
}
});
} else { // File Request
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
} else {
filePath = path.join(this.UIPath, "public", 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("/");
}
res.sendFile(filePath);
});
}
let publicFile = () => {
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
} else {
filePath = path.join(this.UIPath, "public", url);
}
} else {
let url = req.url
res.sendFile(filePath);
}
let privateSite = () => {
let filePath;
if(url.startsWith("/_")) {
filePath = path.join(this.UIPath, url);
@@ -109,6 +124,16 @@ class Server {
res.sendFile(filePath);
}
if(!this.auth.isLoggedInUser(req, res)) {
if(!url.includes(".")) {
publicPage()
} else {
publicFile()
}
} else {
privateSite()
}
}
logRequest(req, res, next) {
@@ -158,7 +183,7 @@ class Server {
const PORT = 3003;
server.listen(PORT, () => {
console.log("\n")
console.log(chalk.yellow("**************America****************"))
console.log(chalk.yellow("*************** Hyperia ***************"))
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
console.log(chalk.yellow("***************************************"))
console.log("\n")