1, 4: Fixing 404 after logged out, adding market entry
This commit is contained in:
@@ -14,16 +14,18 @@ export default class AuthHandler {
|
||||
}
|
||||
|
||||
isLoggedInUser(req, res) {
|
||||
const token = req.cookies.auth_token; // read cookie
|
||||
const token = req.cookies.auth_token;
|
||||
|
||||
if (!token) {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return true
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
||||
req.user = decoded;
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import QuillDB from "../_/quilldb.js"
|
||||
|
||||
export default class Database extends QuillDB {
|
||||
|
||||
|
||||
get = {
|
||||
user: (id) => {
|
||||
return this.nodes[id]
|
||||
|
||||
@@ -2,6 +2,7 @@ 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';
|
||||
@@ -20,11 +21,13 @@ class Server {
|
||||
db;
|
||||
auth;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
DBPath = path.join(__dirname, '../db')
|
||||
|
||||
registerRoutes(router) {
|
||||
// router.post('/api/location', handlers.updateLocation)
|
||||
router.post('/login', this.auth.login)
|
||||
router.get('/signout', this.auth.logout)
|
||||
router.get('/db/images/*', this.getUserImage)
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
@@ -49,24 +52,49 @@ class Server {
|
||||
}
|
||||
}
|
||||
|
||||
getUserImage = async (req, res) => {
|
||||
function getFileByNumber(dir, number) {
|
||||
const files = fs.readdirSync(dir);
|
||||
const match = files.find(file => {
|
||||
const base = path.parse(file).name; // filename without extension
|
||||
return base === String(number);
|
||||
});
|
||||
return match ? path.join(dir, match) : null;
|
||||
}
|
||||
let filePath = getFileByNumber(path.join(this.DBPath, "images"), path.basename(req.url))
|
||||
|
||||
res.sendFile(filePath)
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
if(!this.auth.isLoggedInUser(req, res)) {
|
||||
console.log("Not logged in")
|
||||
let url = req.url
|
||||
if(url === "/") {
|
||||
url = "/index.html"
|
||||
} else if(!url.includes(".")) { // TODO: Make public app single-page
|
||||
url = path.join("/pages", url) + ".html"
|
||||
}
|
||||
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(this.UIPath, url);
|
||||
} else {
|
||||
filePath = path.join(this.UIPath, "public", url);
|
||||
}
|
||||
if(!url.includes(".")) { // Page request
|
||||
if(url === "/") {
|
||||
url = "/index.html"
|
||||
} else {
|
||||
url = path.join("/pages", url) + ".html"
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
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);
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
}
|
||||
} else {
|
||||
let url = req.url
|
||||
|
||||
|
||||
Reference in New Issue
Block a user