fix saveFile & remove dotenv

- fix saveFile on publish using non-existent username
- asyncify some DB calls
- remove dotenv
This commit is contained in:
Hugh Rundle 2023-01-11 13:30:52 +11:00
parent 40f6ade790
commit 91cb731433
5 changed files with 22 additions and 35 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
.env rundev
*.db *.db

18
package-lock.json generated
View file

@ -1,18 +1,17 @@
{ {
"name": "soyuz-web", "name": "soyuz-web",
"version": "1.0.0", "version": "0.1.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "soyuz-web", "name": "soyuz-web",
"version": "1.0.0", "version": "0.1.0",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"dependencies": { "dependencies": {
"better-sqlite3": "^8.0.1", "better-sqlite3": "^8.0.1",
"better-sqlite3-session-store": "^0.1.0", "better-sqlite3-session-store": "^0.1.0",
"body-parser": "^1.20.1", "body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"sprightly": "^1.0.3" "sprightly": "^1.0.3"
@ -260,14 +259,6 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==",
"engines": {
"node": ">=12"
}
},
"node_modules/ee-first": { "node_modules/ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@ -1255,11 +1246,6 @@
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.1.tgz",
"integrity": "sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w==" "integrity": "sha512-463v3ZeIrcWtdgIg6vI6XUncguvr2TnGl4SzDXinkt9mSLpBJKXT3mW6xT3VQdDN11+WVs29pgvivTc4Lp8v+w=="
}, },
"dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
"integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ=="
},
"ee-first": { "ee-first": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",

View file

@ -4,12 +4,15 @@
"description": "Web app for publishing Gemini posts", "description": "Web app for publishing Gemini posts",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"dev": "./rundev",
"password-reset": "node cli.js password-reset", "password-reset": "node cli.js password-reset",
"user-create": "node cli.js user-create" "user-create": "node cli.js user-create"
}, },
"author": "Hugh Rundle <hugh@hughrundle.net> (https://www.hughrundle.net/)", "author": "Hugh Rundle <hugh@hughrundle.net> (https://www.hughrundle.net/)",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"keywords": ["gemini"], "keywords": [
"gemini"
],
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/hughrun/soyuz-web.git" "url": "https://github.com/hughrun/soyuz-web.git"
@ -18,7 +21,6 @@
"better-sqlite3": "^8.0.1", "better-sqlite3": "^8.0.1",
"better-sqlite3-session-store": "^0.1.0", "better-sqlite3-session-store": "^0.1.0",
"body-parser": "^1.20.1", "body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"sprightly": "^1.0.3" "sprightly": "^1.0.3"

View file

@ -1,4 +1,3 @@
require('dotenv').config()
const express = require('express') const express = require('express')
const { getLatestPost, getNow, publishNewPost, requireLoggedIn, resetPassword, saveFile, updatePost, verifyUser, getSavedFile } = require('./utilities') const { getLatestPost, getNow, publishNewPost, requireLoggedIn, resetPassword, saveFile, updatePost, verifyUser, getSavedFile } = require('./utilities')
@ -6,8 +5,8 @@ const { getLatestPost, getNow, publishNewPost, requireLoggedIn, resetPassword, s
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const Database = require('better-sqlite3'); const Database = require('better-sqlite3');
const session = require('express-session') const session = require('express-session')
const sprightly = require('sprightly');
const SqliteStore = require("better-sqlite3-session-store")(session) const SqliteStore = require("better-sqlite3-session-store")(session)
const sprightly = require('sprightly');
// configure Express // configure Express
const app = express() const app = express()
@ -108,7 +107,8 @@ app.post('/publish', requireLoggedIn, (req, res) => {
}) })
app.post('/save', requireLoggedIn, (req, res) => { app.post('/save', requireLoggedIn, (req, res) => {
saveFile(req.session.user, req.body.textarea, () => { saveFile(req.session.user.username, req.body.textarea,)
.then( () => {
res.redirect('/') res.redirect('/')
}) })
}) })

View file

@ -75,13 +75,13 @@ const updateLatestPostDate = function(username, callback) {
} }
// AUTHORISATION MIDDLEWARE // AUTHORISATION MIDDLEWARE
const verifyUser = function (req, res, next) { const verifyUser = async function (req, res, next) {
let username = req.body.username let username = req.body.username
let password = req.body.password let password = req.body.password
let stmt = db.prepare( let stmt = db.prepare(
'SELECT * FROM users WHERE username = ?' 'SELECT * FROM users WHERE username = ?'
) )
user = stmt.get(username) user = await stmt.get(username)
if (!user) { if (!user) {
return next() return next()
@ -147,11 +147,10 @@ const publishNewPost = function(req, cb) {
}) })
}) })
// clear any saved post now that it is published // clear any saved post now that it is published
saveFile(req.session.user.username, '# Title of my note', () => { saveFile(req.session.user.username, '# Title of my note').then( () => {
// delete active page on db and in session return updateLatestPostDate(req.session.user.username, datestring => {
updateLatestPostDate(req.session.user.username, datestring => { req.session.user.latest_post = datestring
req.session.user.latest_post = datestring return cb()
return cb()
}) })
}) })
} }
@ -280,25 +279,25 @@ let updatePost = function(req, callback) {
}) })
} }
let saveFile = function(user, text, callback) { let saveFile = async function(user, text) {
let stmt = db.prepare( let stmt = db.prepare(
'UPDATE users SET saved_post = ? WHERE username = ?' 'UPDATE users SET saved_post = ? WHERE username = ?'
); );
stmt.run(text, user.username); saved = await stmt.run(text, user);
callback() return saved
} }
let getSavedFile = function(user) { let getSavedFile = async function(user) {
let stmt = db.prepare( let stmt = db.prepare(
'SELECT saved_post FROM users WHERE username = ?' 'SELECT saved_post FROM users WHERE username = ?'
) )
stmt.pluck(true) stmt.pluck(true)
let post = stmt.get(user) let post = await stmt.get(user)
return post return post
} }
// TODO: // TODO:
let savePictures = function(text) { let savePictures = async function(text) {
// we will need to save pictures to the server // we will need to save pictures to the server
// separately when publishing // separately when publishing
} }