trying stuff out

This commit is contained in:
Mike 2025-03-31 16:34:30 -04:00
parent 245597c494
commit 4be0ea5870
9 changed files with 50 additions and 37 deletions

View file

@ -4,13 +4,15 @@
"": { "": {
"name": "KeyNotes.App", "name": "KeyNotes.App",
"dependencies": { "dependencies": {
"connect-sqlite3": "^0.9.15", "KeyNotes.App": ".",
"ejs": "^3.1.10", "bcryptjs": "latest",
"express": "^4.21.2", "connect-sqlite3": "latest",
"express-session": "^1.18.1", "ejs": "latest",
"express-validator": "^7.2.1", "express": "latest",
"passport": "^0.7.0", "express-session": "latest",
"passport-local": "^1.0.0", "express-validator": "latest",
"passport": "latest",
"passport-local": "latest",
}, },
}, },
}, },
@ -23,6 +25,8 @@
"@tootallnate/once": ["@tootallnate/once@1.1.2", "", {}, "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw=="], "@tootallnate/once": ["@tootallnate/once@1.1.2", "", {}, "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw=="],
"KeyNotes.App": ["KeyNotes.App@file:", { "dependencies": { "KeyNotes.App": ".", "bcryptjs": "^3.0.2", "connect-sqlite3": "^0.9.15", "ejs": "^3.1.10", "express": "^4.21.2", "express-session": "^1.18.1", "express-validator": "^7.2.1", "passport": "^0.7.0", "passport-local": "^1.0.0" } }],
"abbrev": ["abbrev@1.1.1", "", {}, "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q=="], "abbrev": ["abbrev@1.1.1", "", {}, "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q=="],
"accepts": ["accepts@1.3.8", "", { "dependencies": { "mime-types": "~2.1.34", "negotiator": "0.6.3" } }, "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw=="], "accepts": ["accepts@1.3.8", "", { "dependencies": { "mime-types": "~2.1.34", "negotiator": "0.6.3" } }, "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw=="],
@ -49,6 +53,8 @@
"base64-js": ["base64-js@1.5.1", "", {}, "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="], "base64-js": ["base64-js@1.5.1", "", {}, "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="],
"bcryptjs": ["bcryptjs@3.0.2", "", { "bin": { "bcrypt": "bin/bcrypt" } }, "sha512-k38b3XOZKv60C4E2hVsXTolJWfkGRMbILBIe2IBITXciy5bOsTKot5kDrf3ZfufQtQOUN5mXceUEpU1rTl9Uog=="],
"bindings": ["bindings@1.5.0", "", { "dependencies": { "file-uri-to-path": "1.0.0" } }, "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ=="], "bindings": ["bindings@1.5.0", "", { "dependencies": { "file-uri-to-path": "1.0.0" } }, "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ=="],
"bl": ["bl@4.1.0", "", { "dependencies": { "buffer": "^5.5.0", "inherits": "^2.0.4", "readable-stream": "^3.4.0" } }, "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w=="], "bl": ["bl@4.1.0", "", { "dependencies": { "buffer": "^5.5.0", "inherits": "^2.0.4", "readable-stream": "^3.4.0" } }, "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w=="],
@ -421,6 +427,8 @@
"yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="], "yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="],
"KeyNotes.App/KeyNotes.App": ["KeyNotes.App@file:.", {}],
"agent-base/debug": ["debug@4.4.0", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA=="], "agent-base/debug": ["debug@4.4.0", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-6WTZ/IxCY/T6BALoZHaE4ctp9xm+Z5kY/pzYaCHRFeyVhojxlrm+46y68HA6hr0TcwEssoxNiDEUJQjfPZ/RYA=="],
"cacache/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], "cacache/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="],

5
messages/cookies.txt Normal file
View file

@ -0,0 +1,5 @@
# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_localhost FALSE / FALSE 1743975320 connect.sid s%3Af8SoTtWAPvSfsvf-HvFZAE8ZZlwI2QDl.57i%2FjcW237eV8Q3d8Q3PrNJzpoC0I%2BQ6K%2BNacTtOqJI

View file

@ -2,6 +2,8 @@
"name": "KeyNotes.App", "name": "KeyNotes.App",
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"KeyNotes.App": ".",
"bcryptjs": "^3.0.2",
"connect-sqlite3": "^0.9.15", "connect-sqlite3": "^0.9.15",
"ejs": "^3.1.10", "ejs": "^3.1.10",
"express": "^4.21.2", "express": "^4.21.2",

View file

@ -1,7 +1,7 @@
function usernameValidation(username) { function usernameValidation(username) {
const errors = []; const errors = [];
if (username.length < 5) { if (username.length < 5) {
errors.push("*username must be at least 8 characters"); errors.push("*username must be at least 5 characters");
} }
if (username.includes(" ")) { if (username.includes(" ")) {

View file

@ -1,8 +1,7 @@
const fs = require("node:fs");
const { logging } = require("./middlewares/logging"); const { logging } = require("./middlewares/logging");
const path = require("node:path"); const path = require("node:path");
const express = require("express"); const express = require("express");
const passport = require("passport"); const passport = require("./middlewares/auth");
const session = require("express-session"); const session = require("express-session");
const SQLiteStore = require("connect-sqlite3")(session); const SQLiteStore = require("connect-sqlite3")(session);
@ -17,25 +16,24 @@ app.use(express.static(assetsPath));
app.set("views", path.join(__dirname, "views")); app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs"); app.set("view engine", "ejs");
app.use( app.use(
session({ session({
store: new SQLiteStore({ dir: "./src/db/", db: "keynotes.db" }), store: new SQLiteStore({ dir: "./src/db/", db: "keynotes.db" }),
secret: "keynotes.app", secret: "keynotes",
cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 }, cookie: { maxAge: 7 * 24 * 60 * 60 * 1000, sameSite: "strict" },
saveUninitialized: false, saveUninitialized: false,
resave: false, resave: false,
}), }),
); );
app.use(passport.session()); // app.use(passport.session());
app.use(passport.authenticate("session"));
app.use(express.urlencoded({ extended: false })); app.use(express.urlencoded({ extended: false }));
app.use(logging); app.use(logging);
app.use((req, res, next) => { app.use((req, res, next) => {
res.locals.currentUser = req.user; res.locals.currentUser = req.user;
next(); next();
}); });
app.use("/", indexRouter); app.use("/", indexRouter);
app.use("/auth", authRouter); app.use("/auth", authRouter);

View file

@ -3,6 +3,11 @@ const db = require("../models/query");
const { validationResult } = require("express-validator"); const { validationResult } = require("express-validator");
function loginGet(req, res, next) { function loginGet(req, res, next) {
console.log(req.user, req.session.cookie);
console.table(req.session);
if (!req.user && req.session.id) {
req.session.id = null;
}
res.render("login", { res.render("login", {
pageTitle: "InspiredCliches | Login", pageTitle: "InspiredCliches | Login",
errors: req.session.messages, errors: req.session.messages,
@ -10,6 +15,7 @@ function loginGet(req, res, next) {
} }
function logOut(req, res, next) { function logOut(req, res, next) {
res.clearCookie("connect.sid", { path: "/" });
req.logout((err) => { req.logout((err) => {
if (err) return next(err); if (err) return next(err);
}); });

View file

@ -1,11 +1,11 @@
const db = require("../models/query"); const db = require("../models/query");
function indexGet(req, res, next) { function indexGet(req, res) {
if (res.locals.currentUser) { if (res.locals.currentUser) {
const keynotes = db.getEveryNote(); const keynotes = db.getEveryNote();
const userLikedPosts = db.getLikesByUser(res.locals.currentUser.user_id); const userLikedPosts = db.getLikesByUser(res.locals.currentUser.user_id);
res.render("feed", { return res.render("feed", {
pageTitle: "InspiredCliches | Feed", pageTitle: "InspiredCliches | Feed",
keynotes: keynotes, keynotes: keynotes,
userLikedPosts: userLikedPosts, userLikedPosts: userLikedPosts,
@ -14,7 +14,7 @@ function indexGet(req, res, next) {
res.render("home", { pageTitle: "InspiredCliches" }); res.render("home", { pageTitle: "InspiredCliches" });
} }
function addLike(req, res, next) { function addLike(req, res) {
const { noteId } = req.query; const { noteId } = req.query;
const noteExits = noteId ? db.getNoteById(noteId) : null; const noteExits = noteId ? db.getNoteById(noteId) : null;
@ -30,15 +30,14 @@ function addLike(req, res, next) {
res.redirect("/"); res.redirect("/");
} }
function addNotePost(req, res, next) { function addNotePost(req, res) {
const { message, media } = req.body; const { message, media } = req.body;
console.log("add note post");
if (res.locals.currentUser) { if (res.locals.currentUser) {
const userId = res.locals.currentUser.user_id; const userId = res.locals.currentUser.user_id;
db.putNewNote({ message: message, media: null, userId: userId }); db.putNewNote({ message: message, media: null, userId: userId });
} }
res.redirect("/"); res.redirect("/");
} }
@ -55,11 +54,14 @@ function deleteNote(req, res) {
} }
} }
function addNoteGet(req, res, next) { function addNoteGet(req, res) {
res.render("note-form", { pageTitle: "InspiredCliches | New Note" }); if (res.locals.currentUser)
return res.render("note-form", { pageTitle: "InspiredCliches | New Note" });
res.redirect("/");
} }
function getProfile(req, res, next) { function getProfile(req, res) {
const { userId } = req.query; const { userId } = req.query;
const userExists = userId ? db.getUserById(userId) : null; const userExists = userId ? db.getUserById(userId) : null;

View file

@ -1,24 +1,15 @@
const { Database } = require("bun:sqlite"); const { Database } = require("bun:sqlite");
const fs = require("node:fs"); const fs = require("node:fs");
const path = require("node:path"); const path = require("node:path");
const { mkdir } = require("node:fs/promises");
const { stat } = require("node:fs");
const dbDirPath = path.join(path.dirname(__dirname), "/db"); const dbDirPath = path.join(path.dirname(__dirname), "db");
const dbPath = path.join(dbDirPath, "/keynotes.db"); const dbPath = path.join(dbDirPath, "/keynotes.db");
async function makeDirectory(path) { if (!fs.existsSync(dbDirPath)) {
const dirCreation = await mkdir(path); fs.mkdirSync(dbDirPath);
return dirCreation; console.log(`Created DB Directory at ${dbDirPath}`);
} }
// Make sure DB path exists
stat(dbDirPath, (err, stats) => {
if (err !== null) {
makeDirectory(dbDirPath);
}
});
const db = new Database(dbPath); const db = new Database(dbPath);
// Enable WAL Mode // Enable WAL Mode
db.exec("PRAGMA journal_mode = WAL;"); db.exec("PRAGMA journal_mode = WAL;");

View file

@ -1,4 +1,5 @@
const db = require("./db"); const db = require("./db");
console.log(`${db.filename} test`);
function getEveryNote() { function getEveryNote() {
try { try {