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

18
package-lock.json generated
View file

@ -1,18 +1,17 @@
{
"name": "soyuz-web",
"version": "1.0.0",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "soyuz-web",
"version": "1.0.0",
"version": "0.1.0",
"license": "AGPL-3.0-or-later",
"dependencies": {
"better-sqlite3": "^8.0.1",
"better-sqlite3-session-store": "^0.1.0",
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-session": "^1.17.3",
"sprightly": "^1.0.3"
@ -260,14 +259,6 @@
"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": {
"version": "1.1.1",
"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",
"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": {
"version": "1.1.1",
"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",
"main": "server.js",
"scripts": {
"dev": "./rundev",
"password-reset": "node cli.js password-reset",
"user-create": "node cli.js user-create"
},
"author": "Hugh Rundle <hugh@hughrundle.net> (https://www.hughrundle.net/)",
"license": "AGPL-3.0-or-later",
"keywords": ["gemini"],
"keywords": [
"gemini"
],
"repository": {
"type": "git",
"url": "https://github.com/hughrun/soyuz-web.git"
@ -18,7 +21,6 @@
"better-sqlite3": "^8.0.1",
"better-sqlite3-session-store": "^0.1.0",
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-session": "^1.17.3",
"sprightly": "^1.0.3"

View file

@ -1,4 +1,3 @@
require('dotenv').config()
const express = require('express')
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 Database = require('better-sqlite3');
const session = require('express-session')
const sprightly = require('sprightly');
const SqliteStore = require("better-sqlite3-session-store")(session)
const sprightly = require('sprightly');
// configure Express
const app = express()
@ -108,7 +107,8 @@ app.post('/publish', 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('/')
})
})

View file

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